spring mvc 相对struts2来说开发效率较高,性能来说也较高。
需要重点说明的是Struts2里边可以省略后缀类似http://xxxx.action 可以省略action
但是spring mvc不能省略必须带后缀http://xxx.action
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app id="StoreQuery" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>StoreQuerySpringMVC Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/appContext.xml</param-value> </context-param> <!-- spring mvc 拦截器 --> <servlet> <servlet-name>app</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>app</servlet-name> <!-- 如果配置成/*则是拦截所有但是5.0版本测试必须加/app/*才可以, 会出现404错误, 因为返回来的JSP还是会被拦截,就会报错了! --> <!-- / 也是拦截所有的,但是除了专门配置的静态文件除外--> <!-- *.do这里我们配置成只拦截后缀为.do的页面 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
spring-mvc.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解配置 扫描com.storequery.action包下的所有类 driven默认加载映射处理器和适配器--> <context:component-scan base-package="com.storequery.action"/> <mvc:annotation-driven> </mvc:annotation-driven> <!-- JSP视图解析器 给返回页面加上前缀和后缀以匹配正确的路径 --> <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> <!-- 这里是XML配置 --> <!-- <bean id="urlMapping" --> <!-- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> --> <!-- <property name="mappings"> --> <!-- <props> --> <!-- 类似Struts2里边的action对应的类 --> <!-- <prop key="login.do">loginAction</prop> --> <!-- </props> --> <!-- </property> --> <!-- </bean> --> <!-- <bean id="loginAction" class="com.storequery.action.LoginAction"> --> <!-- </bean> --> </beans>
Action类配置
package com.storequery.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; //注解 @Controller @RequestMapping("/loginController.do") //action多的话可以进行分类 (/log/) public class LoginAction { //同样的这里可以(login) 访问时就是/lon/login.do @RequestMapping(params="method=login") public String login(String username, String password) { System.out.println("我进来了"); System.out.println(username); return "login/login_success"; } //return "redirect:xxx.do" } //XML配置 //public class LoginAction implements org.springframework.web.servlet.mvc.Controller { // // @Override // public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // // TODO Auto-generated method stub // System.out.println("我进来了"); // String username = request.getParameter("username"); // System.out.println(username); // return new ModelAndView("login/login_success"); // } // //}
重定向 redirect时 rul发生变化,request里设置的数据会清空
forward转发 url不变,request不清空
JSP页面内容
<form action="loginController.do" method="post"> <input type="hidden" name="method" value="login">//提交不同的方法时可以用这个 <div class="login-ic"> <i ></i> <input type="text" name="username" value="用户" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'User name';}"/> <div class="clear"> </div> </div> <div class="login-ic"> <i class="icon"></i> <input type="password" name="password" value="密码" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'password';}"/> <div class="clear"> </div> </div> <div class="log-bwn"> <input type="submit" value="Login" > </div> <br> <div id="errorDiv"></div> </form>学到什么再补充