• 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

    2.配置web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>springmvc</display-name>
     4   
     5   
     6   <servlet>
     7         <servlet-name>action</servlet-name>
     8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     9         <init-param>
    10             <param-name>contextConfigLocation</param-name>
    11             <param-value>classpath:spring-mvc.xml</param-value>
    12         </init-param>
    13         <load-on-startup>1</load-on-startup>
    14     </servlet>
    15     
    16     <servlet-mapping>
    17         <servlet-name>action</servlet-name>
    18         <url-pattern>*.action</url-pattern>
    19     </servlet-mapping>
    20   
    21   <welcome-file-list>
    22     <welcome-file>index.htm</welcome-file>
    23     <welcome-file>index.jsp</welcome-file>
    24   </welcome-file-list>
    25 </web-app>

    3.配置 spring-mvc.xml 名字要与web.xml中的 classpath:后面的保持一致就可以了

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans                            
     5                         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
     6     
     7 <!--     handdlerMapping 如果没有写 默认是 BeanNameUrlHandlerMapping -->
     8 <!--         映射分为3种,BeanNameUrlHandlerMapping、simpleUrlHandlerMapping、controllerClassNameHandlerMapping -->
     9 <!--     property name="order" value="1" 用于排序哪种优先级高,数字越小优先级越高 -->
    10     <!-- 处理器映射 -->
    11     <!-- BeanNameUrl处理器映射 -->
    12     <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    13         <property name="order" value="1"/>
    14     </bean>
    15     <!-- SimpleUrl处理器映射, 多个不同的url转到同个action请求的情况 -->
    16     <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    17         <property name="mappings">
    18             <props>
    19                 <prop key="/hi.action">hc</prop>
    20                 <prop key="/a.action">hc</prop>
    21                 <prop key="/b.action">hc</prop>
    22                 <prop key="/c.action">hc</prop>
    23             </props>
    24         </property>
    25         <property name="order" value="0"/>
    26     </bean>
    27     <!-- ControllerClassName处理器映射 -->
    28 <!--     根据类名请求url,类名要小写,切记 -->
    29     <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    30         <property name="order" value="2"/>
    31     </bean>
    32                         
    33     <!-- 控制器 -->
    34     <bean id="hc" name="/hi.action" class="cn.itcast.springmvc.web.controller.HomeController"/>
    35     <bean id="personCommandController" class="cn.itcast.springmvc.web.controller.PersonCommandController"/>
    36     <bean id="personFormController" class="cn.itcast.springmvc.web.controller.PersonFormController">
    37         <!-- 表单视图 -->
    38         <property name="formView" value="jPersonCreate"/>
    39         <!-- 成功视图 -->
    40         <property name="successView" value="index"/>
    41     </bean>
    42     <bean id="myWizardController" class="cn.itcast.springmvc.web.controller.MyWizardController">
    43         <property name="pages">
    44             <list>
    45                 <value>wizard/jPersonBaseInfo</value>
    46                 <value>wizard/jPersonEduInfo</value>
    47                 <value>wizard/jPersonWorkInfo</value>
    48             </list>
    49         </property>
    50     </bean>
    51     
    52     
    53     <!-- 内部资源视图解析器  prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->
    54 <!--     用于解析在controller 中return  ModelAndView中的视图名的拼凑 -->
    55     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    56         <!-- 前缀 -->
    57         <property name="prefix" value="/WEB-INF/jsps/"/>
    58         <!-- 后缀 -->
    59         <property name="suffix" value=".jsp"/>
    60     </bean>
    61     
    62 </beans>

    4.HomeController.java

     1 package cn.itcast.springmvc.web.controller;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.springframework.web.servlet.ModelAndView;
     7 import org.springframework.web.servlet.mvc.AbstractController;
     8 
     9 public class HomeController extends AbstractController {
    10 
    11     @Override
    12     protected ModelAndView handleRequestInternal(HttpServletRequest request,
    13             HttpServletResponse response) throws Exception {
    14 
    15 
    16         System.out.println("hello" + request.getContextPath() + request.getServletPath());
    17         
    18         return new ModelAndView("index");
    19     }
    20 
    21 }

    5 . index.jsp

     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <html>
     3   <head>
     4     <title>My JSP 'index.jsp' starting page</title>
     5   </head>
     6   
     7   <body>
     8     
     9     This is my JSP page. spring MVC<br>
    10   </body>
    11 </html>
    View Code

    spring-mvc.xml中包含3种handdlerMapping的配置 选择一种适合的

    接下来进行Controller的介绍

  • 相关阅读:
    Java导出Excel和CSV(简单Demo)
    ffmepg命令行参数
    VLC命令参数(转载)
    深入Java虚拟机读书笔记第五章Java虚拟机
    JS常用方法记录
    记一次数据库的优化
    Infobright数据库使用
    Mysql连接驱动8.0版本改动
    Eclipse新建SrpingBoot项目Pom.xml文件报错
    SpringBoot 热部署开发
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3800993.html
走看看 - 开发者的网上家园