复习第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;
    }
}
posted @ 2023-01-13 15:08  jsqup  阅读(12)  评论(0编辑  收藏  举报