SpringBoot 参数传递与网页展现
参数传递我们最常见的就是在url后通过?/&两个符号来将参数传递到后台,当然springboot也是也一样,我们可以通过这种方式将参数传递到后台,那么后台如何接收这些参数呢?下面我们一起学习一下: 这里我们将用到@RequestParam注解,这个注解有三个参数分别是:value、required、defaultValue,具体的用法,下面一一为大家介绍。
@RequestMapping(value = "/par1", method = RequestMethod.GET) public String reqPar1(@RequestParam("name") String name){ return name; }
通过@RequestParam注解声明接收用户传入的参数,这样当我们在浏览器输入http://localhost:8080/par1?name=123
@RequestMapping(value = "/par2", method = RequestMethod.GET) public String reqPar2(@RequestParam(value = "name", required = false) String name){ if(null != name){ return name; }else{ return "未传入参数"; } }
我们看到第一个接口我们并没有写value和required,其实第一个接口是简写,等同于
@RequestParam(value = "name", required = true)
required=true:该参数不能为空;相反required=false:该参数能为空
@RequestMapping(value = "/par3", method = RequestMethod.GET) public String reqPar3(@RequestParam(value = "name", defaultValue = "null") String name){ return name; }
最后说一下defaultValue看字面意思,估计你已经想到它的作用了,是的当我们未穿入该参数时的默认值。 下面我们先看一下博客园中博客地址的链接:http://www.cnblogs.com/AndroidJotting/p/8232686.html,请大家注意红色位置,这样的参数传递是不是很有趣,我们并不用设置参数的key,那么这是怎么实现的呢?请接着看。
@RequestMapping(value = "/par4/{id}", method = RequestMethod.GET) public Integer reqPar4(@PathVariable("id") Integer id){ return id; }
这样是不是和博客园的访问很像,这样我们便可以直接将传递参数加在url后面。最后再来活学活用一下:
@RequestMapping(value = "/{id}/par5", method = RequestMethod.GET) public Integer reqPar5(@PathVariable("id") Integer id){ return id; }
OK到这里关于参数传递的内容就和大家分享完毕,最后再给大家补充一个小知识: resources资源springboot默认只映射static、templates两个文件夹下的文件,那么如何进行拓展呢?很简单,比如我们在resources下新建一个image资源,这是我们需要打开项目的主类:xxApplication
@SpringBootApplication public class Springboot1Application extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(Springboot1Application.class, args); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); //这种方式会在默认的基础上增加/image/**映射到classpath:/image/,不会影响默认的方式,可以同时使用。 registry.addResourceHandler("/image/**") .addResourceLocations("classpath:/image/"); } }这样简单一配置,我们就完成了上面的需求。
SpringBoot 简易展现网页SpringBoot 通过controller 来实现网页展现:
@RequestMapping(value = "/index1") public ModelAndView index1() { ModelAndView model =new ModelAndView(); model.setViewName("/NewFile.html"); return model; }
需要使用到ModelAndView 对象,NewFile的网页存放在webapp目录下。
阅读更多- 关于SpringBoot自定义注解(解决post接收String参数 null(前台传递json格式))
- SpringBoot入坑-请求参数传递
- SpringBoot 415状态参数传递问题
- springboot 关于controller层传递单个参数的校验
- springboot解决传递json字符串时获取某个参数为null
- SpringBoot中Rest风格接口传递多个参数
- Spring Boot/VUE中路由传递参数的实现代码
- springboot 中使用 AOP 打印 类名、方法名、方法参数名称和方法参数值
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- ios--网页js调用oc代码+传递参数+避免中文参数乱码的解决方案(实例)
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
- 使用validator-api来验证spring-boot的参数
- boke练习: springboot整合springSecurity出现的问题,传递csrf
- Spring 参数传递
- Spring AOP切面的时候参数的传递
- web-view h5页面传递参数给小程序,小程序接收h5网页传过来的数据——微信web-view高级用法10
- spring boot 设置tomcat post参数限制
- 使用validator-api来验证spring-boot的参数
- springboot中使用定时任务,异步调用,自定义配置参数(八)
- 通过post方式传递json参数,并返回网页源码