一、利用xml 配置
1.web.xml
<
web-app
version
=
"2.4"
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
display-name
>Spring MVC Application</
display-name
>
<
servlet
>
<
servlet-name
>mymvc-dispatcher</
servlet-name
>
<!--声明mvc控制器DispatcherServlet-->
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<!--声明mvc配置文件-->
<!--如果不配置该选项,则默认扫描 ‘servlet-name’-servlet.xml文件 ,
以该web.xml为例,如不配置,默认访问 mymvc-dispatcher-servlet.xml-->
<
init-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>WEB-INF/mymvc.xml</
param-value
>
</
init-param
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>mymvc-dispatcher</
servlet-name
>
<!--请求路径-->
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
2.mymvc.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" >
<mvc:annotation-driven />
<context:component-scan base-package="com"/>
<import resource="myjpa.xml"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
二、利用java 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.springapp.mvc; 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; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.UrlBasedViewResolver; /** * Created by zzq on 2017/11/30. */ @Configuration @EnableWebMvc @ComponentScan (basePackages = "com" ) public class MvcConfig{ @Bean public InternalResourceViewResolver setupViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix( "/WEB-INF/pages/" ); resolver.setSuffix( ".jsp" ); // resolver.setViewClass(JstlView.class); return resolver; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.springapp.mvc; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * Created by zzq on 2017/11/30. */ @Configuration @ComponentScan (basePackages = "com" ,excludeFilters = { @ComponentScan .Filter(type = FilterType.ANNOTATION,value = EnableWebMvc. class )}) public class RootConfig { } |
package
com.springapp.mvc;
import
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Created by zzq on 2017/11/30.
*/
public
class
SpringMvcWebIn
extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected
Class<?>[] getRootConfigClasses() {
return
new
Class<?>[]{RootConfig.
class
};
}
@Override
protected
Class<?>[] getServletConfigClasses() {
return
new
Class<?>[]{MvcConfig.
class
};
}
@Override
protected
String[] getServletMappings() {
return
new
String[]{
"/"
};
}
}