首页 > 代码库 > springmvc
springmvc
springmvc请求流程与struts比较
1.添加包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
spring-aop-3.2.2.jar
spring-aspects-3.2.2.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-context-support-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.aopalliance</groupId> <artifactId>com.springsource.org.aopalliance</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>com.springsource.org.apache.commons.logging</artifactId> <version>1.1.1</version> </dependency> <!-- 添加spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.5.RELEASE</version> </dependency> </dependencies>
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.在src/main/resources 配置 spring-mvc.xml 名字要与web.xml中的 classpath:后面的保持一致就可以了
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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-3.2.xsd"> <!-- handdlerMapping 如果没有写 默认是 BeanNameUrlHandlerMapping --> <!-- 映射分为3种,BeanNameUrlHandlerMapping、simpleUrlHandlerMapping、controllerClassNameHandlerMapping --> <!-- property name="order" value="http://www.mamicode.com/1" 用于排序哪种优先级高,数字越小优先级越高 --> <!-- 处理器映射 --> <!-- BeanNameUrl处理器映射 --> <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="order" value="1"/> </bean> <!-- SimpleUrl处理器映射, 多个不同的url转到同个action请求的情况 --> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hi.action">hc</prop> <prop key="/a.action">hc</prop> <prop key="/b.action">hc</prop> <prop key="/c.action">hc</prop> </props> </property> <property name="order" value="0"/> </bean> <!-- ControllerClassName处理器映射 --> <!-- 根据类名请求url,类名要小写,切记 --> <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="order" value="2"/> </bean> <!-- 控制器 --> <bean id="hc" name="/hi.action" class="cn.itcast.springmvc.web.controller.HomeController"/> <bean id="personCommandController" class="cn.itcast.springmvc.web.controller.PersonCommandController"/> <bean id="personFormController" class="cn.itcast.springmvc.web.controller.PersonFormController"> <!-- 表单视图 --> <property name="formView" value="jPersonCreate"/> <!-- 成功视图 --> <property name="successView" value="index"/> </bean> <bean id="myWizardController" class="cn.itcast.springmvc.web.controller.MyWizardController"> <property name="pages"> <list> <value>wizard/jPersonBaseInfo</value> <value>wizard/jPersonEduInfo</value> <value>wizard/jPersonWorkInfo</value> </list> </property> </bean> <!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp--> <!-- 用于解析在controller 中return ModelAndView中的视图名的拼凑 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsps/"/> <!-- 后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>
4.HomeController.java
package cn.itcast.springmvc.web.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("hello" + request.getContextPath() + request.getServletPath()); return new ModelAndView("index"); } }
5 . index.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> This is my JSP page. spring MVC<br> </body> </html>
spring-mvc.xml中包含3种handdlerMapping的配置 选择一种适合的
下一篇Controller的介绍
springmvc
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。