controller层中,参数的获取方式以及作用域的问题
package com.krry.web; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; import bean.User; @Controller
@RequestMapping("/model")
public class ModelMapController extends BaseController {
/********参数获取的方式**************************/ //http://localhost/krryxa/model/hanlder/1.html
@RequestMapping("/handler/{id}")
public String handler(@PathVariable("id")Integer id){
//获得参数id为1
return "redirect:/success.jsp";
} //http://localhost/krryxa/model/handler2.html?id=5
@RequestMapping("/handler2")
public String handler2(Integer id){
//获得参数id为5
System.out.println(id);
return "redirect:/success.jsp";
} //通过对象的的注入方式最好
//http://localhost/krryxa/model/handler3.html?username=1351
@RequestMapping("/handler3")
public String handler3(User user){
//获得参数username为1351
System.out.println(user.getUsername());
return "redirect:/success.jsp";
} //http://localhost/krryxa/model/handler4.html?id=5
@RequestMapping("/handler4")
public String handler4(@ModelAttribute("teacher")User user){//若sessiong域中teacher改变了,这里也会改变
//获得参数id为5
System.out.println(request.getParameter("id"));
return "redirect:/success.jsp";
} /**作用域的问题reuqest session application 以下作用域的范围都是:request**/ //在index页面直接用${message}获取
@RequestMapping("/handler7")
public String handler7(ModelMap map){
//这里是map的addAttribute设置
map.addAttribute("message", "我爱你吗。你们爱我我吗");
return "model/index";
} //在index页面直接用${message}获取
@RequestMapping("/handler5")
public String handler5(){
request.setAttribute("message", "我爱你吗。你们爱我我吗");
return "model/index";
} //在index页面直接用${message}获取
@RequestMapping("/handler6")
public ModelAndView handler6(){
//视图和作用域融合体
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("model/index"); //跳转到这个页面
modelAndView.addObject("message", "是打发是大法师的发送到发士大夫阿什顿");
return modelAndView;
}
//在index页面直接用${user.username}获取
@RequestMapping("/handler8")
public String handler8(@ModelAttribute("user")User user){
user.setUsername("ModelAttribute 我爱你吗。你们爱我吗");
return "model/index";
} }
controller层中,参数的获取方式以及作用域的问题的更多相关文章
- Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比
1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...
- token获取在controller层中
集合判断是否为空 注意:token获取在controller层中,token中存的所有数据都要在controller中获取 在自己的接口里调用别的接口需要判断一下返回值是否为空
- SpringMVC之请求参数的获取方式
转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求 ...
- DRF 序列化组件 模型层中参数补充
一. DRF序列化 django自带有序列化组件,但是相比rest_framework的序列化较差,所以这就不提django自带的序列化组件了. 首先rest_framework的序列化组件使用同fr ...
- SpringMVC请求参数的获取方式
一.GET请求参数获取 1. 通过HttpServletRequest获取参数 2. 直接方法参数获取 3. RequestParam注解方式获取请求参数 4. Bean方式获取参数 5. Model ...
- SpringMVC在Controller层中注入request的坑
记一次为了节省代码没有在方法体中声明HttpServletRequest,而用autowire直接注入所钻的坑 结论 给心急的人. 直接在Controller的成员变量上使用@Autowire声明Ht ...
- nodejs中命令行中参数的获取和解析
首先,假设有如下的命令行 node abc.js arg1 arg2 arg3,现在想在abc.js中获取后面的参数arg1.arg2.arg3… var args = process.argv.s ...
- controller函数中参数列表使用多个@RequestBody
首先出现这种情况是因为有下面这种需求 $.ajax({ type: "POST", url: "${pageContext.request.contextPath}/co ...
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
随机推荐
- Windows Server 2012开启多人远程
首先在Server Roles中选择Remote Desktop Services,然后在Role Services中安装Remote Desktop Session Host 安装完成后需要重启机器 ...
- 基于Docker构建带有Rsync的Jenkins
1.编辑Dockerfile文件 FROM jenkins USER root ADD sources.list /etc/apt/sources.list RUN apt-get update &a ...
- python学习:字典排序
按字典值排序 按照字典value排序,类似sort -k 命令 import operator x= {1:2,3:4,4:3,2:1,0:0} sorted_x = sorted(x.ite ...
- docker 实践(一)
docker 简介 容器虚拟化,比传统的虚拟化轻量 2013年出现,发展非常迅猛 Redhat在6.5版本开始支持docker 使用go语言开发,基于apache2.0协议 开源软件,项目代码在git ...
- Sql Server 常用事务处理总结
在数据库操作中,常用事务写法: 1. 通过 @@error 判断一批sql 执行完毕,是否有异常. @@error 为系统变量,每次执行完 sql 都会返回一个数值, 0 表示 执行成功 ,非0 ...
- thinkPHP替换SQL变量
使用tp里M()->where(pb_id=%d and course=%d and DATE_FORMAT(pub_time, \"%H:%i:%s\") < &qu ...
- [UWP]做个调皮的BusyIndicator
1. 前言 最近突然想要个BusyIndicator.做过WPF开发的程序员对BusyIndicator应该不陌生,Extended WPF Toolkit 提供了BusyIndicator的开源实现 ...
- js中的回调函数
1.你定义的 2.你没有调用 3.但是最终他执行了 例子: 定时器回调函数 setTimeout(function(){ },100); dom元素的回调函数 document.getElementB ...
- js使用defineProperty的一些坑
var p2={ }; Object.defineProperty(p2,"gs",{ get:function () { return this.gs; }, set:funct ...
- Windows下nexus-3.*搭建Maven私服
1.下载 从官网下载https://help.sonatype.com/display/NXRM3/Download 选择Windows archive https://download.sonaty ...