SpringMVC-two(springMVC的入门配置)
02【掌握】springMVC的入门配置
1.创建项目并导包
2.创建log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3.创建springmvc.xml [springmvc的配置文件]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</beans>
4.创建User
5.创建UserController
/**
* 自定义控制器
* @author LJH
*
*/
public class User01Controller implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<User> users=new ArrayList<>();
for (int i = 1; i <=10; i++) {
users.add(new User(i, "小明"+i, "武汉"+i, new Date()));
}
ModelAndView modelAndView=new ModelAndView();
//设置要跳转的路径
modelAndView.setViewName("list.jsp");
//reqest.getRequestDispatcher("list.jsp")
//设置数据
modelAndView.addObject("users", users);//
request.setAttribute("users",users)
return modelAndView;
}
}
6.创建index.jsp
7.创建list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>
所有用户
</h1>
<hr>
<table border="1" width="100%" cellpadding="5" cellspacing="5">
<tr>
<th>编号</th>
<th>姓名</th>
<th>地址</th>
<th>生日</th>
</tr>
<c:forEach var="sn" items="${users }">
<tr>
<td align="center">${sn.id }</td>
<td align="center">${sn.name }</td>
<td align="center">${sn.address }</td>
<td align="center">
<fmt:formatDate value="${sn.birth }" pattern="yyyy-MM-dd
HH:mm:ss"/>
</td>
</tr>
</c:forEach>
</table>
<fmt:formatNumber value="1999.8899912312" pattern="#.##" >
</fmt:formatNumber>
</body>
</html>
8.修改springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 声明控制器 -->
<bean id="user01Controller" name="/user01Controller.action"
class="com.sxt.controller.User01Controller"></bean>
<!-- 配置处理器映射器
有两个处理器映射器
1:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping把bean的name做为url进行查找,也就是handler必须配置name属性
2:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 里面可以配置属性【后面讲】
-->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置处理器适配器
有两个处理器适配器
1:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter写的
controller要实现Controller的接口
2:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter 写controler要实
现HttpRequestHandler接口【后面说】
-->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
<!-- 文件上传处理 器 -->
<!-- 拦截器 -->
</beans>
9.修改web.xml配置前端控制器
<!-- 配置springmvc的前端控制器开始 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<!-- 注入配置文件的路径 spirngmvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<!-- 容器启动时创建对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析
第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需
要配置不让DispatcherServlet进行解析
使用此种方式可以实现 RESTful风格的url
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置springmvc的前端控制器结束 -->