步骤:1.定义一个handler处理器,并且实现controller接口
package jk.handlers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
importorg.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.Controller;
//定义处理器
public class SomeHandler implements Controller {
@Override
publicModelAndView handleRequest(HttpServletRequest request,
HttpServletResponseresponse) throws Exception {
// 以下语句等价于=request.setAttribute("message","HelloSpringMVC World!")
ModelAndViewmv = new ModelAndView();
mv.addObject("message","Hello SpringMVC World!");
mv.setViewName("/WEB-INF/welcome.jsp");
returnmv;
}
}
2.下一步定义的跳转的界面定义到web-inf页面下,比较其他页面下,相对安全性更高,不能重定向,只能通过java代码实现跳转
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>01.SpringMvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.注册somehandler容器xml:springMvc
Id必须/开头,当然ID=name
<%@page language="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome page <br>
${message }<br>
${requestScope.message}<br>
</body>
</html>
4.定义一个中央调度器(中央控制器,前端控制器)DispatchServlet
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册处理器 -->
<beanid="/some.do" class="jk.handlers.SomeHandler">
<!--id必须开头-->
</bean>
</beans>