基於配置文件的web項目維護起來可能會更方便,但是有時候我們會有一些特殊的需求,比如防止客戶胡亂更改配置,這時候我們需要給配置隱藏到代碼中。
1.創建一個動態web項目(無需web.xml)
2.右鍵項目添加幾個package: com.easyweb.config (保存項目配置) com.easyweb.controller (保存springMvc controller)
3.在 com.easyweb.config 新建一個類 WebApplicationStartup ,這個類實現WebApplicationInitializer 接口,是項目的入口,作用類似於web.xml,具體代碼如下:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
<code
class
=
"hljs"
java=
""
>
package
com.easyweb.config;
import
javax.servlet.MultipartConfigElement;
import
javax.servlet.ServletContext;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRegistration.Dynamic;
import
org.springframework.web.WebApplicationInitializer;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import
org.springframework.web.servlet.DispatcherServlet;
/**
* 服務器啟動入口類
*
* @author Administrator
*
*/
public
class
WebApplicationStartup
implements
WebApplicationInitializer {
private
static
final
String SERVLET_NAME = Spring-mvc;
private
static
final
long
MAX_FILE_UPLOAD_SIZE =
1024
*
1024
*
5
;
// 5 Mb
private
static
final
int
FILE_SIZE_THRESHOLD =
1024
*
1024
;
// After 1Mb
private
static
final
long
MAX_REQUEST_SIZE = -1L;
// No request size limit
/**
* 服務器啟動調用此方法,在這里可以做配置 作用與web.xml相同
*/
@Override
public
void
onStartup(ServletContext servletContext)
throws
ServletException {
// 注冊springMvc的servlet
this
.addServlet(servletContext);
// 注冊過濾器
// servletContext.addFilter(arg0, arg1)
// 注冊監聽器
// servletContext.addListener(arg0);
}
/**
* 注冊Spring servlet
*
* @param servletContext
*/
private
void
addServlet(ServletContext servletContext) {
// 構建一個application context
AnnotationConfigWebApplicationContext webContext = createWebContext(SpringMVC.
class
, ViewConfiguration.
class
);
// 注冊spring mvc 的 servlet
Dynamic dynamic = servletContext.addServlet(SERVLET_NAME,
new
DispatcherServlet(webContext));
// 添加springMVC 允許訪問的Controller后綴
dynamic.addMapping(*.html, *.ajax, *.css, *.js, *.gif, *.jpg, *.png);
// 全部通過請用 “/”
// dynamic.addMapping(/);
dynamic.setLoadOnStartup(
1
);
dynamic.setMultipartConfig(
new
MultipartConfigElement(
null
, MAX_FILE_UPLOAD_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
}
/**
* 通過自定義的配置類來實例化一個Web Application Context
*
* @param annotatedClasses
* @return
*/
private
AnnotationConfigWebApplicationContext createWebContext(Class<!--?-->... annotatedClasses) {
AnnotationConfigWebApplicationContext webContext =
new
AnnotationConfigWebApplicationContext();
webContext.register(annotatedClasses);
return
webContext;
}
}</code>
|
4.在com.easyweb.config 下添加類 SpringMVC 繼承 WebMvcConfigurerAdapter,這個類的作用是進行SpringMVC的一些配置,代碼如下:
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
30
31
32
33
34
35
|
<code
class
=
"hljs"
java=
""
>
package
com.easyweb.config;
import
org.springframework.context.annotation.ComponentScan;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
//指明controller所在的包名
@ComponentScan
(basePackages = {com.easyweb.controller})
public
class
SpringMVC
extends
WebMvcConfigurerAdapter {
/**
* 非必須
*/
@Override
public
void
configureDefaultServletHandling(
final
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* 如果項目的一些資源文件放在/WEB-INF/resources/下面
* 在瀏覽器訪問的地址就是類似:https://host:port/projectName/WEB-INF/resources/xxx.css
* 但是加了如下定義之后就可以這樣訪問:
* 非必須
*/
@Override
public
void
addResourceHandlers(
final
ResourceHandlerRegistry registry) {
registry.addResourceHandler(/resources
/**/
*).addResourceLocations(/WEB-INF/resources/);
}
}</code>
|
5.添加view配置文件com.easyweb.config下新建類ViewConfiguration,里面可以根據自己的需要定義視圖攔截器:
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
30
31
32
33
34
35
36
37
38
39
40
|
<code avrasm=
""
class
=
"hljs"
>
package
com.easyweb.config;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.ViewResolver;
import
org.springframework.web.servlet.view.JstlView;
import
org.springframework.web.servlet.view.UrlBasedViewResolver;
import
org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import
org.springframework.web.servlet.view.tiles2.TilesView;
@Configuration
public
class
ViewConfiguration {
@Bean
public
ViewResolver urlBasedViewResolver() {
UrlBasedViewResolver viewResolver;
viewResolver =
new
UrlBasedViewResolver();
viewResolver.setOrder(
2
);
viewResolver.setPrefix(/WEB-INF/);
viewResolver.setSuffix(.jsp);
viewResolver.setViewClass(JstlView.
class
);
// for debug envirment
viewResolver.setCache(
false
);
return
viewResolver;
}
@Bean
public
ViewResolver tilesViewResolver() {
UrlBasedViewResolver urlBasedViewResolver =
new
UrlBasedViewResolver();
urlBasedViewResolver.setOrder(
1
);
urlBasedViewResolver.setViewClass(TilesView.
class
);
//urlBasedViewResolver.
return
urlBasedViewResolver;
}
@Bean
public
TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer =
new
TilesConfigurer();
tilesConfigurer.setDefinitions(
new
String[] { classpath:tiles.xml });
return
tilesConfigurer;
}
}</code>
|
6.本例中還用了tiles視圖解析器,替換了原始的include方式