1、配置web.xml
< servlet >
< servlet-name > annomvc </ servlet-name >
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > annomvc </ servlet-name >
< url-pattern > *.page </ url-pattern >
</ servlet-mapping >
2、配置spring的xml
[servlet-name]-servlet.xml -> annomv-servlet.xml
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
< context:component-scan base-package = "mysite.security.controller" />
< context:component-scan base-package = "mysite.**.*" />
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
< bean
class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->
< bean id = "viewResolver"
class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" />
< property name = "prefix" value = "/WEB-INF/jsp/" />
< property name = "suffix" value = ".jsp" />
</ bean >
3、编写Controller
/**
* 使用Controller注解说明这是一个controller bean
*/
@Controller
public class TestController {
/*
* 这个注解说明Spring MVC分发请求时,如果求情的资源是test,则转到这里处理
* 例如:http://localhost:8080/contextpath/test.page
*/
@RequestMapping
(
"test"
)
public
void
main(Model model) {
model.addAttribute(
"date"
,
new
Date());
}
}
4、编写jsp页面进行展示
<%@ page language = "java" contentType = "text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<%@
taglib
prefix
=
"fmt"
uri
=
"http://java.sun.com/jsp/jstl/fmt"
%>
<!
DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=ISO-8859-1"
>
<
title
>
Insert title here
</
title
>
</
head
>
<
body
>
<!-- 控制器组织的数据date,通过 jstl 格式化后显示 -->
现在的时间是:
<
fmt:formatDate
value
=
"
${date}
"
pattern
=
"yyyy年MM月dd日"
/>
</
body
>
</
html
>
5、测试
输入:http://localhost:8080/WebLog/test.page