首页 > 其他分享 >spring mvc获取路径参数的几种方式

spring mvc获取路径参数的几种方式

时间:2022-11-29 13:41:10浏览次数:33  
标签:RequestMapping spring 路径 System name mvc println out String

spring mvc获取路径参数的几种方式 

SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理 以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。在使用注解 的SpringMVC中,处理器Handler是基于@Controller和@RequestMapping这两个注解的,@Controller声明 一个处理器类,@RequestMapping声明对应请求的映射关系,这样就可以提供一个非常灵活的匹配和处理方式。

 

DispatcherServlet是继承自HttpServlet的,既然SpringMVC是基于DispatcherServlet的,那么 我们先来配置一下DispatcherServlet,好让它能够管理我们希望它管理的内容。HttpServlet是在web.xml文件中声明的。


一、从视图向controller传递值,  controller <--- 视图

1、通过@PathVariabl注解获取路径中传递参数 

1​     @RequestMapping(value = "/{id}/{str}") 
2​ public ModelAndView helloWorld(@PathVariable String id,
3​ @PathVariable String str) {
4​ System.out.println(id);
5​ System.out.println(str);
6​ return new ModelAndView("/helloWorld");
7​ }


2、
 
1)简单类型,如int, String, 应在变量名前加@RequestParam注解,
例如:

@RequestMapping("hello3")
public String hello3( @RequestParam("name" ) String name,
@RequestParam("hobby" ) String hobby){
System. out.println("name=" +name);
System. out.println("hobby=" +hobby);
return "hello" ;
}


但这样就要求输入里面必须有这两个参数了,可以用required=false来取消,例如:
@RequestParam(value="name",required=false) String name
但经测试也可以完全不写这些注解,即方法的参数写String name,效果与上面相同。
 
2)对象类型:


​​?

@RequestMapping("/hello4" )
public String hello4(User user){
System.out.println("user.getName()=" +user.getName());
System.out.println("user.getHobby()=" +user.getHobby());
return "hello";
}


Spring MVC会按:
     “HTTP请求参数名=  命令/表单对象的属性名”
    的规则自动绑定请求数据,支持“级联属性名”,自动进行基本类型数据转换。
此外,还可以限定提交方法为POST,即修改方法的@RequestMapping注解为
@RequestMapping(value="/hello4",method=RequestMethod.POST)
 
最后,注意,如果这里提交过来的字符出现乱码,应该在web.xml里加入如下filter:

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name >
<url-pattern>/*</url-pattern>
</filter-mapping>




返回数据到页面几种方式:      

//返回页面参数的第二种方式,在形参中放入一个Model  
@RequestMapping(value = "/hello2.htm")
public String hello2(int id,Model model){
System.out.println("hello2 action:"+id);
model.addAttribute("name", "huangjie");
//这个只有值没有键的情况下,使用Object的类型作为key,String-->string
model.addAttribute("ok");
return "hello";
}
//返回页面参数的第一种方式,在形参中放入一个map  
@RequestMapping(value = "/hello1.htm")
public String hello(int id,Map<String,Object> map){
System.out.println("hello1 action:"+id);
map.put("name", "huangjie");
return "hello";
}




作者:少帅

标签:RequestMapping,spring,路径,System,name,mvc,println,out,String
From: https://blog.51cto.com/u_15683012/5894828

相关文章

  • Spring mvc 返回json格式 - 龙企阁
    第一次使用springmvc,在此也算是记录一下以防忘记,希望有经验的朋友指出不足的地方一、使用maven管理jar。<dependency><groupId>org.codehaus.jackson</groupId><artif......
  • spring中依赖注入与aop讲解
    一、依赖注入这个属于IOC依赖注入,也叫控制反转,IOC是说类的实例由容器产生,而不是我们用new的方式创建实例,控制端发生了改变所以叫控制反转。<?xmlversion="1.0"encoding="U......
  • SpringCloud Alibaba(四) - Nacos 配置中心
    1、环境搭建1.1依赖<!--nacos注册中心注解@EnableDiscoveryClient--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-......
  • SpringMVC异常处理
    异常处理方式* 配置简单异常处理器SimpleMappingExceptionResolver* 配置自定义异常处理器自定义异常处理步骤* 创建异常处理器实现HandlerExceptio......
  • SpringBoot 实际项目开发中工厂模式的巧妙使用
    简单工厂模式:     简单工厂模式是创建型模式,创建型模式顾名思义,也就是说在创建对象的时候,遇到了瓶颈才会选择的设计模式。那么该什么情况使用呢。  简单工厂模式......
  • vuecli3配置文件路径别名
    vue.config.js文件中module.exports={configureWebpack:{resolve:{alias:{'assets':'@/assets','common':'@/common',......
  • Spring Boot测试
    SpringBoot测试一、了解单元测试单元测试(UnitTest)是为了检验程序的正确性。一个单元可能是单个程序、类、对象、方法等,它是应用程序的最小可测试部件。单元测试的必......
  • SpringBoot 2.4.0版本后解决跨域问题
    packagecom.atguigu.gulimall.gateway.config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;impor......
  • springboot报错说 Failed to parse multipart servlet request; nested exception is
    问题:一次开发中遇到一个springboot的异常,如下所示:Failedtoparsemultipartservletrequest;nestedexceptionisjava.io.IOException:Thetemporaryuploadlocatio......
  • SpringBoot2 常用注解
    目录​​SpringBoot2常用注解​​​​@SpringBootApplication​​​​@EnableAutoConfiguration​​​​@ImportResource​​​​@Value​​​​@ConfigurationProperties(......