SpringMVC(工作中常见问题->解决方案)

本文讲解了如何在SpringMVC中使用@RequestParam处理URL传参,并展示了不同类型的参数如何影响数据返回。通过实例演示了url拼接参数和路径变量的使用,适合理解SpringMVC参数处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.传参问题

1.1 url中拼接传参

/**
 * 需求:
 *  测试一下不同参数展示不同的数据:
 *  请求路径: http://localhost:8123/test/ajaxList 不带参数时,显示3条数据
 *           http://localhost:8123/test/ajaxList?type=2 带参数 type=2时 显示2条数据
 *           http://localhost:8123/test/ajaxList?type=1 带参数 type=1时 显示1条数据
 * @param httpServletRequest
 * @param type
 * @return
 */
@RequestMapping("/ajaxList")
@ResponseBody
public List<ValueTest> test1(HttpServletRequest httpServletRequest,@RequestParam(required = false, defaultValue="0") Integer type){
    try{
        if (type == 1){
            List<ValueTest> list = new ArrayList<>();
            list.add(new ValueTest("2020年",451L,"系列1"));
            return list;
        }else if (type == 2){
            List<ValueTest> list = new ArrayList<>();
            list.add(new ValueTest("2013年",352L,"系列1"));
            list.add(new ValueTest("2014年",303L,"系列1"));
            return list;
        }
            List<ValueTest> list = new ArrayList<>();
            list.add(new ValueTest("2021年",451L,"系列1"));
            list.add(new ValueTest("2013年",352L,"系列1"));
            list.add(new ValueTest("2014年",303L,"系列1"));
            return list;

    }catch(Exception e){
        return null;
    }
}

 

知识点: @RequestParam注解用于接收请求url中拼接的参数,其中属性值的含义是:

                required = false 参数可以不传;

               defaultValue="0" 不传参数时,设置默认值,默认值为0;


1.2 路径参数

  • 路径参数类似请求参数,但没有key部分,只是一个值。例如下面的URL: http://localhost:9090/showUser/spring
  •  其中的spring是表示用户的密码字符串。在Spring MVC中,spring被作为路径变量用来发送一个值到服务器。Sping 3以后Spring 3以后支持注解@PathVariable用来接收路径参数。
  • 为了使用路径变量,首先需要在RequestMapping注解的值属性中添加一个变量,该变量必须放在花括号之间,例如:@RequestMapping(value= “/showUser/{pwd}”) 然后在方法签名中加上@PathVariable注解。
  • 具体代码如下:
@RequestMapping(value= "/showUser/{pwd}")
 public String testPathVariable(@PathVariable(name="pwd") String password, Map<String, Object> model){
  model.put("pwd", password);
  return "showUser";
 }

运行结果:

  • 可以在请求映射中使用多个路径变量。例如,下面定义了userId和orderId两个路径变量。@RequestMapping(value= “/showUser/{userId}/{orderId}”);

 


 

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name>spring</display-name> <!-- 加载Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring.xml</param-value> </context-param> <!-- 字符集 过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring监听器 --> <listener> <description>Spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring MVC --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>SpringMVC</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 本次需要做的配置,注意位置,要放在配置springmvc的url-pattern之前 --> <!-- 表示当程序加载符合这些路径的资源时,不会通过dispatchservlet --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.html</url-pattern> <url-pattern>/upload/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Session超时时间 --> <!-- <session-config> <session-timeout>15</session-timeout> </session-config> --> </web-app>
最新发布
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值