zoukankan      html  css  js  c++  java
  • 【SpringMVC】url映射传参

    web.xml

    <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--servlet-class 固定值,将servlet-name注册给 DispatcherServlet -->
            <load-on-startup>1</load-on-startup>
            <!--服务器加载配置文件顺序,有此属性时可以在服务器启动时加载.xml文件,进行文件的校验,能够较快的分析出是代码出错还是配置文件出错-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:spring-mvc.xml</param-value>
            </init-param>
            <!--加载初始配置项,此属性配置可以更改系统默认加载.xml的路径,其中param-name为固定值param-value值为所放xml路径,classpath*表示类路径,可用通配符表示配置文件(*.xml)-->
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>*.do</url-pattern>
            <!--url-pattern 以/开头和以/*结尾的是用来做路径映射的
                          以前缀*.开头用来做扩展的
                          单独/是用来default servlet映射的 
                  -->
        </servlet-mapping>

    spring-mvc.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:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
        <context:component-scan base-package="com.wf.springmvc.controller" />
        <!-- 扫描基包下的所有注解类 -->
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:viewClass="org.springframework.web.servlet.view.JstlView"
            p:prefix="/WEB-INF/view/"
            p:suffix=".jsp"/>
            <!-- 配置视图名称解析器,配置
            p:prefix属性指定controller返回页面的所存放的路径
            p:suffix属性指定页面的类型,他们共同组成返回的路径
            p:viewClass属性是在返回到jsp页面进行解析jstl标签 -->
    </beans>

    配置注解 
    @RequestMapping 路径注解属性,可以注解类也可以注解方法。有value属性(一个参数时默认)指定url路径解析,method属性指定提交方式(默认为get提交) 
    @RequestParam 请求规则注解,value属性匹配前台传递的参数(一个参数时默认),required属性此字段是否必须传值(boolean,默认为true),defaultValue此参数的默认值(存在此参数时,说明前台不必需传递参数,required为false) 
    @PathVariable 注解绑定Url传过来的值到方法的参数上面,此注解一般用于url模板传参。 
    注意RequestParam 与PathVariable 的区别

    1. 方式一 基本的传递
        @RequestMapping("/login")   // url:http://localhost:8080/login/login.do 
        public String login(HttpServletRequest request){
                String name=request.getParameter("name"); // 获取前台传递的参数
            return "start";
        }
     2. 方式二 利用超链接进行跳转传递参数 前台name需要与注解RequestParam的参数保持一致。(传递少许参数)
        @RequestMapping("/login")  //url:http://localhost:8080/login/login.do?name=tom
        public String login(@RequestParam(value="age",required=false,defaultValue="24") String agenum,@RequestParam("name") String name){
            System.out.println(agenum); // 前台参数自动绑定到idmun中
            System.out.println(name); 
            return "start";
        }
     3. 方式三 url模板传参,将参数作为路径的一部分,不建议使用
        @RequestMapping("/login/{name}/{id}")
        // http://localhost:8080/login/login/lisi/m123.do  
        public String login(@PathVariable("name") String name01,@PathVariable("id") String id01){
            System.out.println(name01);
            System.out.println(id01);
            return "start";
        }
     4. 方式四 指定提交方式,RequestMethod类的相应属性(常用)
        @RequestMapping("/check",method=RequestMethod.POST)
        public String check(HttpServletRequest request,HttpServletResponse response ){
            String name=request.getParameter("name");
            String pwd=request.getParameter("pwd");
            if("tom".equals(name) && "1234".equals(pwd)){
                return "success";
            }
            return "error";
        }
    
     5. 方式五 post提交表单 传递模型Model参数 将表单封装为对应的javabean进行传递(常用)
        @RequestMapping(value="/check",method=RequestMethod.POST)
        public String check(User user){
            if("tom".equals(user.getName()) && "1234".equals(user.getPwd())){
                return "success";
            }
            return "error";
         }
    
    
     6. 特殊的 属性编辑器 在前台到后台data日期类型等的转化会出错,此时我们需要属性编辑器进行属性的转化
         //日期传递参数会产生异常,因此在传递时间参数时,需要进行类型转换,在初始化时进行数据的绑定与转化
    
        @RequestMapping(value="/todate/{data}",method=RequestMethod.GET)
        public String todate(@PathVariable("data") Date date){
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            return "start";
        }
        @InitBinder    //初始化参数绑定, 日期类型的转化,
        public void initBinder(ServletRequestDataBinder binder){
            binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
        }

    在拦截的url中,不否定在不同类中有相同路径,例如在StudentController和StudentController中都会有相应的增删改查方法且方法的RequestMapping路径相同,此时我们就可以在类的注解加入相应的RequestMapping。例如路径http://localhost:8080/login/student/add.do 和 http://localhost:8080/login/teacher/add.do

    摘自:请尊重原创 http://blog.csdn.net/sinat_28978689/article/details/52483881

     

  • 相关阅读:
    1.3计算机网络体系结构及OSI七层参考模型与TCP/IP参考模型
    1.2计算机网络性能指标
    1.1数据交换——电路、报文、分组交换
    一、计算机网络概述
    计算机网络随笔序言及索引
    CCF-CSP历年试题详解(不断更新中)
    【python】序列
    算法课-母函数专题
    算法课-大数专题
    算法课-暴力搜索
  • 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/6906634.html
Copyright © 2011-2022 走看看