大佬教程收集整理的这篇文章主要介绍了Spring Boot 学习(一)@RequestAttribute @MatrixVariable @UrlPathHelper,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@GetMapping("/send")
@H_616_3@public@H_616_3@ String getmessage(httpServletrequest request) {
request.setAttribute(@H_616_3@"msg","跳转"); //@H_616_3@设置参数@H_616_3@
request.setAttribute("code",123);
@H_616_3@return@H_616_3@ "forWARD:/success"; //@H_616_3@转发到success请求@H_616_3@
}
@ResponseBody //@H_616_3@注意要加上这个注解,否则会出现500报错@H_616_3@
@GetMapping("/success")
@H_616_3@public@H_616_3@ Map success(@requestAttribute("msg") String msg, //@H_616_3@通过requestAttribute获取请求域中的所有值@H_616_3@
@requestAttribute("code") Integer code,
httpServletrequest request) { @H_616_3@//@H_616_3@获取到send的数据msg@H_616_3@
Object msg1 = request.getAttribute("msg");
Map@H_616_3@<String,Object> map = new@H_616_3@ HashMap<>();
map.put(@H_616_3@"reqMethod_msg",msg1); //@H_616_3@获取原生servlet中的request的值@H_616_3@
map.put("Annotation_msg",msg); //@H_616_3@获取基于注解额request的值@H_616_3@
return@H_616_3@ map;
}@H_616_3@
值得注意的是如果只声明了Controller那么要在需要显示数据在浏览器的时候需要加上注解@ResponseBody@H_616_3@
package@H_616_3@ com.sp.Controller;
@H_616_3@import@H_616_3@ org.springframework.stereotype.Controller;
@H_616_3@import@H_616_3@ org.springframework.web.bind.Annotation.GetMapping;
@H_616_3@import@H_616_3@ org.springframework.web.bind.Annotation.requestAttribute;
@H_616_3@import@H_616_3@ org.springframework.web.bind.Annotation.ResponseBody;
@H_616_3@import@H_616_3@ javax.servlet.http.httpServletrequest;
@H_616_3@import@H_616_3@ java.util.HashMap;
@H_616_3@import@H_616_3@ java.util.Map;
@Controller //这里只声明了Controller注解
@H_616_3@@H_616_3@public@H_616_3@ class@H_616_3@ requestController {
@GetMapping(@H_616_3@"/send")
@H_616_3@public@H_616_3@ String getmessage(httpServletrequest request) {
request.setAttribute(@H_616_3@"msg","跳转");
request.setAttribute(@H_616_3@"code",123);
@H_616_3@return@H_616_3@ "forWARD:/success"; //@H_616_3@转发到success请求@H_616_3@
}
@ResponseBody //因此在显示页面数据需要加上注解ResponseBody@H_616_3@
@GetMapping(@H_616_3@"/success")
@H_616_3@public@H_616_3@ Map success(@requestAttribute("msg") String msg, //@H_616_3@通过requestAttribute获取请求域中的所有值@H_616_3@
@requestAttribute("code") Integer code,
httpServletrequest request) { @H_616_3@//@H_616_3@获取到send的数据msg@H_616_3@
Object msg1 = request.getAttribute("msg");
Map@H_616_3@<String,Object> map = new@H_616_3@ HashMap<>();
map.put(@H_616_3@"reqMethod_msg",msg1); //@H_616_3@获取原生servlet中的request的值@H_616_3@
map.put("Annotation_msg",msg); //@H_616_3@获取基于注解额request的值@H_616_3@
return@H_616_3@ map;
}
}@H_616_3@
注意在@RestController 包含了@Controller 和 @ResponseBody 这两个注解,所以在访问请求的时候可以显示数据ResponseBody @H_616_3@
Spring Boot 禁用了矩阵变量,需要手动开启
有两种写法在@Configuration(proxyBeanMethods = falsE)注解下
package@H_616_3@ com.sp.config;
@H_616_3@import@H_616_3@ org.springframework.context.Annotation.bean;
@H_616_3@import@H_616_3@ org.springframework.context.Annotation.Configuration;
@H_616_3@import@H_616_3@ org.springframework.web.servlet.config.Annotation.PathMatchConfigurer;
@H_616_3@import@H_616_3@ org.springframework.web.servlet.config.Annotation.WebMvcConfigurer;
@H_616_3@import@H_616_3@ org.springframework.web.util.UrlPathHelper;
@Configuration(proxyBeanMethods @H_616_3@= false@H_616_3@)
@H_616_3@public@H_616_3@ class@H_616_3@ WebConfig /*@H_616_3@implements WebMvcConfigurer @H_616_3@*/@H_616_3@{
@H_616_3@//@H_616_3@ 写法一
@H_616_3@//@H_616_3@ @Override
@H_616_3@//@H_616_3@ public void configurePathMatch(PathMatchConfigurer configurer) { @H_616_3@//@H_616_3@对configurePathMatch进行重写
@H_616_3@//@H_616_3@ UrlPathHelper urlPathHelper = new UrlPathHelper();
@H_616_3@//@H_616_3@ urlPathHelper.setRemoveSemicolonContent(false); @H_616_3@//@H_616_3@将RemoveSemicolonContent设置为false,此时矩阵变量才能生效
@H_616_3@//@H_616_3@ configurer.setUrlPathHelper(urlPathHelper);
@H_616_3@//@H_616_3@ }
@H_616_3@//@H_616_3@ 写法二@H_616_3@
@Bean //自己定义一个组件
@H_616_3@public@H_616_3@ WebMvcConfigurer webMvcConfigurer() {
@H_616_3@return@H_616_3@ new@H_616_3@ WebMvcConfigurer() {
@H_616_3@public@H_616_3@ void@H_616_3@ configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper @H_616_3@= new@H_616_3@ UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(@H_616_3@false@H_616_3@);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}@H_616_3@
@H_616_3@
在UrlPathHelper中默认情况下是移除分号内容
// low=34;brand=byd,audi,yd@H_616_3@// SpringBoot默认是禁用了矩阵变量的功能@H_616_3@// 手动开启:对于路径的处理,UrlPathHelper进行解析@H_616_3@// UrlPathHelper -> private Boolean removeSemicolonContent = true; (移除分号内的内容)@H_616_3@// 在@Configuration中重写configurePathMatch方法,将removeSemicolonContent改为false@H_616_3@注意:之后在controller中,注意这里的矩阵变量要绑定在路径变量中所以加上{path},否则出现404@H_616_3@
@GetMapping("/cars/{path}") @H_616_3@//矩阵变量必须有url路径变量才能被解析,否则404
public@H_616_3@ Map carsell(@matrixVariable("low") Integer low, //@H_616_3@不是请求参数,而是矩阵变量,需要加上注解,矩阵变量要绑定在路径变量中@H_616_3@ @matrixVariable("brand") List<String> brand, @PathVariable(@H_616_3@"path") String path) { //@H_616_3@需要加上路径访问变量的注解@H_616_3@ Map<String,Object> map = new@H_616_3@ HashMap<>(); map.put(@H_616_3@"low",low); map.put(@H_616_3@"brand",brand); map.put(@H_616_3@"path",path); @H_616_3@return@H_616_3@ map; }@H_616_3@
<@H_616_3@a @H_616_3@href@H_616_3@="/cars/sell;low=34;brand=byd,audi,yd"@H_616_3@>@H_616_3@matrixVariable(矩阵变量)</@H_616_3@a@H_616_3@>@H_616_3@
/boss/1;age=34/2;age=22 boss1 路径下的age 2路径下的age,需要注意的是,传递的参数名都一样的时候,需要在矩阵变量的注解中加上pathVar来加以区分,否则会出现404@H_616_3@
@GetMapping("/boss/{BossID}/{empID}") @H_616_3@public@H_616_3@ Map bossmessage(@matrixVariable(value = "age",pathVar = "bossId") Integer bossage, //@H_616_3@当传入的参数名一样时,需要哟波pathVar进行区分@H_616_3@ @matrixVariable(value = "age",pathVar = "empId") Integer empagE) { Map@H_616_3@<String,Object> map = new@H_616_3@ HashMap<>(); map.put(@H_616_3@"bossage",bossagE); map.put(@H_616_3@"empage",empagE); @H_616_3@return@H_616_3@ map; }@H_616_3@
<@H_616_3@a @H_616_3@href@H_616_3@="/boss/1;age=34/2;age=22"@H_616_3@>@H_616_3@matrixVariable(矩阵变量2)</@H_616_3@a@H_616_3@>@H_616_3@
以上是大佬教程为你收集整理的Spring Boot 学习(一)@RequestAttribute @MatrixVariable @UrlPathHelper全部内容,希望文章能够帮你解决Spring Boot 学习(一)@RequestAttribute @MatrixVariable @UrlPathHelper所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。