• Spring MVC体系结构(五)


    一、spring mvc框架搭建步骤

      1、导入jar文件

        spring-web-3.2.0.RELEASE

        spring-webmvc-3.2.0.RELEASE

      2、在web.xml中配置servlet和其他参数

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 在web.xml中配置前端控制器,DispatcherServlet是spring最和核心的类 -->
      <servlet>
              <servlet-name>DispatcherServlet</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:applicationContext.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
              <servlet-name>DispatcherServlet</servlet-name>
              <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <!-- 通过过滤器设置字符编码 -->
      <filter>
              <filter-name>CharacterEncodingFilter</filter-name>
              <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
              <init-param>
                  <param-name>encoding</param-name>
                  <param-value>utf-8</param-value>
              </init-param>
      </filter>
      <filter-mapping>
              <filter-name>CharacterEncodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>

      3、配置applicationContext.xml 

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
        
        <context:component-scan base-package="com.action"/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
        
    </beans>

      4、创建controller--->>处理请求的控制器

    package com.action;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.entity.User;
    
    @Controller
    public class UserAction {
        
        @RequestMapping("submit")
        public ModelAndView show(User user){
            ModelAndView m = new ModelAndView("list");
            m.addObject("uname",user.getUname());
            m.addObject("upass",user.getUpass());
            return m;
        }
    }

     二、controller控制器中参数传递的几种方式

      1、可以通过已经创建的javabean注入

    import com.entity.User;
    
    @Controller
    public class UserAction {
        
        @RequestMapping("submit")
        public ModelAndView show(User user){
            ModelAndView m = new ModelAndView("list");
            m.addObject("uname",user.getUname());
            m.addObject("upass",user.getUpass());
            return m;
        }
    }

      2、对应名称自动填充

    @RequestMapping("submit")
        public ModelAndView show(String uname,String upass,HttpServletRequest request){
            ModelAndView m = new ModelAndView("list");
            m.addObject("uname",uname);//需要和前台的name名称一致
            m.addObject("upass",upass);//需要和前台的name名称一致
            return m;
        }

      3、名称不一致的情况需要添加注解描述

    @RequestMapping("submit")
        /**
         * 针对前台name的名称与参数名称不一致的情况下使用
         * value  参数名
         * required  是否必须
         * defaultValue  如果为空,就当前默认值
         */
        public ModelAndView show(@RequestParam(value="uname",required=false,defaultValue="xxx") String uname,String upass){
            ModelAndView m = new ModelAndView("list");
            m.addObject("uname",uname);//需要和前台的name名称一致
            m.addObject("upass",upass);//需要和前台的name名称一致
            return m;
        }
  • 相关阅读:
    项目经理如何管理团队
    正则表达式判断中文字符
    售前工作经验总结
    项目管理知识体系九大知识领域
    项目经理如何调动员工的积极性
    项目经理与客户沟通的宜与忌
    关于CodeSmith生成CSLA代码的联想
    接触CSLA框架的感觉
    C#多线程浅接触二、
    WF工作流开发回顾:介绍
  • 原文地址:https://www.cnblogs.com/newbest/p/9210846.html
Copyright © 2020-2023  润新知