springmvc第六个练习(当请求中参数名和形参名不一样时,@RequestParam)

校正请求参数名 @RequestParam
       所谓校正请求参数名,是指若请求 URL 所携带的参数名称与处理方法中指定的参数名 不相同时,则需在处理方法参数前,添加一个注解 @RequestParam(“ 请求参数名 ”) ,指定请 URL 所携带参数的名称。该注解是对处理器方法参数进行修饰的。 value 属性指定请求参 数的名称。
项目结果

1.index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>提交参数给Controller</p>
<form action="receiveproperty.do" method="post">
    姓名:<input type="text" name="name"/><br>
    年龄:<input type="text" name="age"/><br>
    <input type="submit" value="提交参数"/>
</form>
<br>

<p>请求参数名和处理器方法的形参名不一样</p>
<form action="receiveparam.do" method="post">
    姓名:<input type="text" name="rname"><br>
    年龄:<input type="text" name="rage"><br>
    <input type="submit" value="提交参数">
</form>

</body>
</html>

2.show.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>show.jsp</p>
<p>/WEB-INF/view/show.jsp从request作用域获取数据</p>
<p>myname数据:${myname}</p>
<p>myage数据:${myage}</p>

</body>
</html>

3.MyController类

package com.it.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping(value = "/receiveproperty.do")
    public ModelAndView doSome(String name,Integer age){
        ModelAndView mv=new ModelAndView();
        mv.addObject("myname",name);
        mv.addObject("myage",age);
        mv.setViewName("show");
        return mv;
    }



    /**
     *请求中参数名和处理器方法的形参名不一样
     * @RequestParam:解决请求中参数名和形参名不一样的问题
     * 属性:1.value请求中参数名称
     *      2.required 是一个boolean,默认是true,true表示请求中必须包含此参数。
     * 位置:在处理器方法的形参定义的前面
     */
    @RequestMapping(value = "/receiveparam.do")
    public ModelAndView receiveParam(@RequestParam(value = "rname") String name,
                                     @RequestParam(value = "rage") Integer age){
        ModelAndView mv=new ModelAndView();
        mv.addObject("myname",name);
        mv.addObject("myage",age);
        mv.setViewName("show");
        return mv;
    }

}

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

<!--
1.声明,注册springmvc核心对象DispatcherServlet
需要在tomcat服务器启动后,创建  DispatcherServlet对象的实例,
因为DispatcherServlet在他的创建过程中,会同时创建springmvc容器对象,
读取springmvc的配置文件时,可以把配置文件都创建好当用户发起请求时就可以直接访问对象


2.servlet初始化会执行init()方法,DispatcherServlet在init()中{
//创建容器,读取配置文件
WebApplicationContext wac=new ClassPathXmlApplicationContext("springmvc.xml");
//把容器对象放入到ServletContext中
getServletContext().setAttribute(key,wac);
}
-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--自定义springmvc读取的配置文件的位置-->
    <init-param>
<!--   springmvc的配置文件的位置属性-->
        <param-name>contextConfigLocation</param-name>
<!--   指定自定义文件的位置-->
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>

<!-- 在tomcat启动后,创建Servlet对象
load-on-startup:表示tomcat启动后创建对象的顺序,它的值是整数,数值越小,tomcat创建对象的时间越早(大于等于0的整数)
-->
    <load-on-startup>1</load-on-startup>

</servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
<!--
使用框架的时候,url-pattern可以使用两种值
1.使用扩展名的方式,语法 *.xxxx,xxxx是自定义的扩展名,常用的方法 *.do,*.action,*.mvc等等。
2.使用斜杠"/"
-->
        <url-pattern>*.do</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>
        <!-- 强制请求对象(HttpServletRequest)使用 encoding编码的值  -->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--强制应答对象(HttpServletResponse)使用encoding编码的值 -->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

5.springmvc.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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 声明组件扫描器-->
    <context:component-scan base-package="com.it.controller"/>


<!--声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--   前缀:视图文件的路径-->
        <property name="prefix" value="/WEB-INF/view/"/>
<!--    后缀:视图文件的扩展名-->
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

项目结果

 

当不使用@RequestParam时,遇到请求中参数的名称和形参名不一样的情况,点击提交后,数据传送不过去。

 

当使用@RequestParam时,遇到请求中参数的名称和形参名不一样的情况,点击提交后,数据可以正常传送

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值