复习第3点-3.获取请求数据
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@Controller
public class GetDataController {
// 访问路径:http://localhost:8080/springmvc_war_exploded/getData?name=jsq
@RequestMapping("/getData")
public ModelAndView getRequestParameter(HttpServletRequest request) {
String name = request.getParameter("name");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
modelAndView.addObject("username", name);
return modelAndView;
}
// 访问路径:http://localhost:8080/springmvc_war_exploded/addUsers?username=jsq&userage=22
@RequestMapping("/addUsers")
public ModelAndView addUsers(String username,int userage){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
modelAndView.addObject("name",username);
modelAndView.addObject("age",userage);
return modelAndView;
}
// 访问路径:http://localhost:8080/springmvc_war_exploded/addUsers2?age=2
@RequestMapping("/addUsers2")
public ModelAndView addUsers2(
@RequestParam(value = "name",required = true,defaultValue = "oldlu") String username,
@RequestParam("age") int userage
){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
modelAndView.addObject("name",username);
modelAndView.addObject("age",userage);
return modelAndView;
}
}
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/17049727.html