不用web.xml,而使用java类配置SpringMVC-阿里云开发者社区

开发者社区> 浅色夏墨> 正文

不用web.xml,而使用java类配置SpringMVC

简介: 不用web.xml,而使用java类配置SpringMVC DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.
+关注继续查看

不用web.xml,而使用java类配置SpringMVC

DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servlet 3规范和Spring 3.1的功能增强, 可以采用一种全新的,更简洁的方式配置Spring MVC了. 下面按这种方式一个Hello World的MVC配置.

Step 1:先用idea创建一个Maven的WEB工程. pom.xml文件如下:

img_e5ee5da711dbdf7b099b00954fb4428f.png
image

OcrWebAppInitializer:

package com.home.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;


public class OcrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {



 @Override

 protected Class<?>[] getRootConfigClasses() {

 return new Class<?>[] { RootConfig.class };

 }



 @Override

 protected Class<?>[] getServletConfigClasses() {

 return new Class<?>[] { WebConfig.class };        //ָ指定Web配置类

 }



 @Override

 protected String[] getServletMappings() {    //将DispatcherServlet映射到"/"

 return new String[] { "/" };

 }



}


RootConfig

package com.home.config;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.ComponentScan.Filter;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.FilterType;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;



@Configuration

@ComponentScan( basePackages={"com.home"},

 excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}

)



public class RootConfig {



}

WebConfig

package com.home.config;



import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.ViewResolver;

import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration

@EnableWebMvc                                //启动SpringMVC

@ComponentScan("com.home.controller")            //启动组件扫描

public class WebConfig extends WebMvcConfigurerAdapter {



 //配置JSP视图解析器

 @Bean

 public ViewResolver viewResolver() {

 InternalResourceViewResolver resolver = new InternalResourceViewResolver();

 resolver.setPrefix("/WEB-INF/views/");

 resolver.setSuffix(".jsp");

 resolver.setExposeContextBeansAsAttributes(true);

 return resolver;

 }



 //配置静态资源的处理

 @Override

 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

 configurer.enable();        //对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet

 }


}

HomeController

package com.home.controller;

import static org.springframework.web.bind.annotation.RequestMethod.*;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;



@Controller

public class HomeController {

 @RequestMapping(value = "/home", method=GET)

 public String home() {

 return "home";

 }

}

home.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

 <title>Spring MVC Tutorial chry</title>

 <style type="text/css">

 </style>

</head>

<body>

<br>

<div style='text-align:center;'>

 index page!

</div>

</body></pre>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>



 <groupId>com</groupId>

 <artifactId>springMVC-config</artifactId>

 <version>1.0-SNAPSHOT</version>

 <packaging>war</packaging>



 <name>springMVC-config Maven Webapp</name>

 <!-- FIXME change it to the project's website -->

 <url>http://www.example.com</url>



 <properties>

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 <maven.compiler.source>1.7</maven.compiler.source>

 <maven.compiler.target>1.7</maven.compiler.target>

 </properties>



 <dependencies>

 <dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.11</version>

 <scope>test</scope>

 </dependency>



 <dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.11</version>

 <scope>test</scope>

 </dependency>





 <dependency>

 <groupId>org.slf4j</groupId>

 <artifactId>slf4j-api</artifactId>

 <version>1.7.12</version>

 </dependency>

 <dependency>

 <groupId>ch.qos.logback</groupId>

 <artifactId>logback-core</artifactId>

 <version>1.1.1</version>

 <scope>provided</scope>

 </dependency>

 <!--实现slf4j接口并整合-->

 <dependency>

 <groupId>ch.qos.logback</groupId>

 <artifactId>logback-classic</artifactId>

 <version>1.1.1</version>

 <scope>provided</scope>

 </dependency>



 <!--druid==>阿里巴巴数据库连接池-->

 <dependency>

 <groupId>com.alibaba</groupId>

 <artifactId>druid</artifactId>

 <version>1.0.25</version>

 </dependency>

 !--2.dao框架:MyBatis依赖--

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis</artifactId>

 <version>3.3.0</version>

 </dependency>

 <!--mybatis自身实现的spring整合依赖-->

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis-spring</artifactId>

 <version>1.3.2</version>

 </dependency>



 <!--3.Servlet web相关依赖-->

 <dependency>

 <groupId>taglibs</groupId>

 <artifactId>standard</artifactId>

 <version>1.1.2</version>

 </dependency>

 <dependency>

 <groupId>jstl</groupId>

 <artifactId>jstl</artifactId>

 <version>1.2</version>



 </dependency>

 <dependency>

 <groupId>com.fasterxml.jackson.core</groupId>

 <artifactId>jackson-databind</artifactId>

 <version>2.5.4</version>

 </dependency>



 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>javax.servlet-api</artifactId>

 <version>3.1.0</version>

 <scope>provided</scope>

 </dependency>



 !--4:spring依赖--

 <!--1)spring核心依赖-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-core</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-beans</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-context</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--2)spring dao层依赖-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-jdbc</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-tx</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--3)springweb相关依赖-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-web</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-webmvc</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--4)spring test相关依赖-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-test</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>mysql</groupId>

 <artifactId>mysql-connector-java</artifactId>

 <version>5.1.35</version>

 <scope>runtime</scope>

 </dependency>



 <!--Aop要导入的包-->

 <dependency>

 <groupId>org.aspectj</groupId>

 <artifactId>aspectjweaver</artifactId>

 <version>1.8.9</version>

 </dependency>

 <!-- https://mvnrepository.com/artifact/org.json/json -->

 <dependency>

 <groupId>org.json</groupId>

 <artifactId>json</artifactId>

 <version>20170516</version>

 </dependency>





 <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->

 <dependency>

 <groupId>org.thymeleaf</groupId>

 <artifactId>thymeleaf-spring4</artifactId>

 <version>3.0.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>javax.validation</groupId>

 <artifactId>validation-api</artifactId>

 <version>2.0.1.Final</version>

 </dependency>

 <dependency>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 <version>18.0</version>

 </dependency>

 <dependency>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 <version>18.0</version>

 </dependency>

 <dependency>

 <groupId>org.hibernate</groupId>

 <artifactId>hibernate-validator</artifactId>

 <version>5.3.6.Final</version>

 </dependency>





 <!-- jsp -->

 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>javax.servlet-api</artifactId>

 <version>3.0.1</version>

 </dependency>

 <dependency>

 <groupId>javax.servlet.jsp</groupId>

 <artifactId>jsp-api</artifactId>

 <version>2.1</version>

 <scope>provided</scope>

 </dependency>

 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>jstl</artifactId>

 <version>1.2</version>

 </dependency>

 <!-- jsp -->

 </dependencies>



 <build>

 <finalName>springMVC-config</finalName>

 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

 <plugins>

 <plugin>

 <artifactId>maven-clean-plugin</artifactId>

 <version>3.0.0</version>

 </plugin>

 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->

 <plugin>

 <artifactId>maven-resources-plugin</artifactId>

 <version>3.0.2</version>

 </plugin>

 <plugin>

 <artifactId>maven-compiler-plugin</artifactId>

 <version>3.7.0</version>

 </plugin>

 <plugin>

 <artifactId>maven-surefire-plugin</artifactId>

 <version>2.20.1</version>

 </plugin>

 <plugin>

 <artifactId>maven-war-plugin</artifactId>

 <version>3.2.0</version>

 </plugin>

 <plugin>

 <artifactId>maven-install-plugin</artifactId>

 <version>2.5.2</version>

 </plugin>

 <plugin>

 <artifactId>maven-deploy-plugin</artifactId>

 <version>2.8.2</version>

 </plugin>

 </plugins>

 </pluginManagement>

 </build>

</project>

这个时候访问相应的路径就可以看到jsp的相关信息了

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Android中Strings.xml使用占位符示例
MainActivity如下: package cn.test; import android.os.Bundle; import android.
779 0
SpringMVC 中配置 Swagger 插件.
一、简介  Swagger的目标是为REST API定义一个与语言无关的标准接口,允许用户发现和理解计算机服务的功能,而无需访问源代码。当通过Swagger正确定义时,用户可以用最少量的实现逻辑理解远程服务并与之交互。
1334 0
SpringBoot实战(三)之使用RestFul Web服务
一、导入maven依赖 4.0.0 org.springframework gs-consuming-rest 0.1.0 org.springframework.
1252 0
SpringBoot项目使用Scala进行Web开发
本文介绍如何在Springboot中使用Scala进行web开发
2609 0
【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能。 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先写一个登录权限过滤类--LoginFilter类实现Filter接口 1 package com.
1782 0
ant使用ftp上传数据时配置的一个报错问题
error:ANT FTP upload file: 425 Unable to build data connection: Connection timed out 以下是xml原配置: &lt;ftp action="put"    server="10.254.21.117"    userid="***"    password="****"    dir
1255 0
你不得不使用的XML代码生成器,那就是XmlFactory
这个XML Factory工具很好。如果你有现成的Xml文档,那么大部分情况下你不需要手工写代码来处理Xml与实体对象间的转换。 博客园这位兄弟有详细的使用情况:http://www.cnblogs.com/rada2002/archive/2011/12/15/2288171.html
542 0
+关注
66
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载