自己配置的第一个基于spring框架的简单登录程序(不含验证,有简单的拦截功能,也没有事务)
简单说下自己的理解,可能有错误欢迎大家指正补充,也希望自己能借此更进一步,谢谢了。
发送请求request----》到达服务器端由servlet容器分派------》相应的servlet处理完返回结果由servlet容器根据结果处理响应-----》response
springMVC中dispacherServlet继承HttpServlet,我们现在web.xml里面配置servlet
1 <servlet> 2 <servlet-name>springmvc</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:springmvc-servlet.xml</param-value> 7 </init-param> 8 <load-on-startup>1</load-on-startup> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet-name>springmvc</servlet-name> 13 <url-pattern>/</url-pattern> 14 </servlet-mapping>
作用域是所有的jsp网页
servlet根据注解通过反射开始执行响应流程
先配置
springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.0.xsd 14 http://www.springframework.org/schema/mvc 15 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 16 17 18 <!-- 自动扫描的包名 --> 19 <context:component-scan base-package="com.zhanghaobo" ></context:component-scan> 20 <!-- 默认的注解映射的支持 --> 21 <mvc:annotation-driven /> 22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 24 <property name="prefix" value="/WEB-INF/jsp/"/> 25 <property name="suffix" value=".jsp"/> 26 </bean> 27 28 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 29 30 <!-- 对静态资源文件的访问 --> 31 <mvc:default-servlet-handler/> 32 <!--作用全局的拦截器--> 33 <mvc:interceptors> 34 <bean class="com.zhanghaobo.interceptor.MyAllInterceptor"></bean> 35 36 </mvc:interceptors> 37 38 </beans>
dispatherservlet通过handlermapping执行响应的操作(interceptor,handler)
拦截器就在此执行操作
识别都是通过注解的方式反射得到的所以我们要将执行的类@Controller
@RequestMapping相当于要struts2中的action配置myFirst对应的class不过这个可以直接作用于方法更灵活,不知道理解的对不对
1 import org.springframework.stereotype.Controller; 2 import org.springframework.ui.Model; 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RequestMethod; 5 6 @Controller 7 public class MyController { 8 @RequestMapping(value="/myFirst",method=RequestMethod.POST) 9 public String hello(String username,String password,Model model){ 10 model.addAttribute("username", username); 11 model.addAttribute("password", password); 12 System.out.println(username+password); 13 return "hello"; 14 } 15 }
拦截器在调用方法前会执行
1 import java.util.Map; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerInterceptor; 7 import org.springframework.web.servlet.ModelAndView; 8 9 public class MyAllInterceptor implements HandlerInterceptor { 10 11 @Override 12 public boolean preHandle(HttpServletRequest request, 13 HttpServletResponse response, Object handler) throws Exception { 14 if("admin".equals(request.getParameter("username"))&&"admin".equals(request.getParameter("password"))){ 15 System.out.println("pre invoke"); 16 System.out.println(handler); 17 return true; 18 } 19 response.sendRedirect("index.jsp"); 20 System.out.println("print agin"); 21 return false; 22 } 23 24 @Override 25 public void postHandle(HttpServletRequest request, 26 HttpServletResponse response, Object handler, 27 ModelAndView modelAndView) throws Exception { 28 Map<String, Object> mm=modelAndView.getModel(); 29 for(String key:mm.keySet()){ 30 System.out.println(key+"-=======================-"); 31 mm.put("username", "张三"); 32 mm.put("password", "sdadsa"); 33 } 34 System.out.println(modelAndView); 35 36 37 } 38 39 @Override 40 public void afterCompletion(HttpServletRequest request, 41 HttpServletResponse response, Object handler, Exception ex) 42 throws Exception { 43 44 System.out.println("over"); 45 } 46 47 }
拦截器要实现HandlerInterceptor,也可以根据需要选择继承(接口适配器)先执行prehandler执行成功才会下步执行响应的controller方法,接着调用posthandler这就是为什么参数里会有一个ModelAndView类型的参数,此类型有个model属性存放参数信息,我们可以打印出来,也可以修改model里面的信息,最后通过视图解析传回客户端。
1 <form action="myFirst" method="post"> 2 username:<input type="text" name="username"><br> 3 password:<input type="password" name="password"><br> 4 <input type="submit" value="submit"> 5 </form>
返回的客户端页面hello.jsp
1 <body> 2 3 user:${username} 4 pass:${password} 5 </body>
页面结果: user:张三 pass:sdadsa
控制台输出结果:
print agin
pre invoke
public java.lang.String com.zhanghaobo.MyController.hello(java.lang.String,java.lang.String,org.springframework.ui.Model)
adminadmin
username-=======================-
password-=======================-
ModelAndView: reference to view with name 'hello'; model is {username=张三, password=sdadsa}
over