具体流程:
@Controller
public class MainController {
@RequestMapping(value = "/page1",)
public String p1(){
return "p1";
}
}
@RequestMapping(value = "/p1/user")
public String user1(){
return "p1";
}
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/p1")
public String user1(){
return "p1";
}
}
@RequestMapping(value = {"/p2","/","/user/*})
public String user2(){
return "p1";
}
@RequestMapping(value = "/getInfo",method = RequestMethod.GET)
public String getInfo(){
return "p1";
}
@RequestMapping(value = "/p3",headers = "token=123123")
public String user3(){
return "p1";
}
@ResponseBody
@RequestMapping(value = "/json")
public List getJson(){
List list = new ArrayList<>();
list.add("aaaaa");
list.add("bbbbb");
return list;
}
如:http://localhost:8080/json?pageId=10
@ResponseBody
@RequestMapping(value = "/json",params = "pageId=10")
public List getJson(){
List list = new ArrayList<>();
list.add("aaaaa");
list.add("bbbbb");
return list;
}
如:/login?username=xxx&password=yyy
@ResponseBody
@RequestMapping(value = "/login")
public PersonBean login(@RequestParam("username") String name,@RequestParam("password")String psw){
PersonBean pb = new PersonBean();
pb.setPname(name);
pb.setPpsw(psw);
return pb;
}
@ResponseBody
@RequestMapping(value = "/login1")
public PersonBean login2(PersonBean pb){
return pb;
}
@ResponseBody
@RequestMapping(value = "/login")
public PersonBean login(@RequestParam("username") String name,@RequestParam(value = "password",required = false)String psw){
PersonBean pb = new PersonBean();
pb.setPname(name);
pb.setPpsw(psw);
return pb;
}
// localhost/j2/page/4
@RequestMapping(value = "/j2/{pageNo}")
public String jump2(@PathVariable("pageNo") int no, Model model){
model.addAttribute("no",no);
return "j2";
}
@RequestMapping(value = "/j1")
public String jump1(@RequestParam("flag") boolean flag){
if (flag){
// 重定向
return "redirect:success";
}else {
// 转发
return "forward:fail";
}
}
@RequestMapping(value = "/success")
public String success(Model model,ModelMap modelMap,Map map){
model.addAttribute("msg","ssss");
modelMap.addAttribute("key","value");
map.put("key1","value1")
return "success";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
Title
成功
${msg},${key},${key1}
@RequestMapping(value = "/fail")
public ModelAndView fail(){
ModelAndView mv = new ModelAndView();
mv.setViewName("fail");
mv.addObject("key3","value3");
return mv;
}
@Controller
public class LoginController {
@RequestMapping("/login")
public String loginView(){
return "login";
}
@RequestMapping("/login1")
public String login(@RequestParam("username") String username,
@RequestParam("password") String psw, Model model){
System.out.println(username+"---"+psw);
if (username.equals("abc")&&psw.equals("123")){
model.addAttribute("username",username);
model.addAttribute("password",psw);
return "success";
}else {
return "fail";
}
}
}