问题描述
我写了一个类UserController:import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.BindingResult;import org.springframework.validation.annotation.Validated;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.bind.annotation.RequestMethod;import com.itech.model.User;@Controller@RequestMapping("/user")public class UserController {private Map<String, User> users=new HashMap<String,User>();public UserController() {users.put("huhuawen1", new User("huhuawen1","29", "123", "123@163.com"));users.put("huhuawen2", new User("huhuawen2","28", "456", "456@163.com"));users.put("huhuawen3", new User("huhuawen3","20", "789", "789@163.com"));}@RequestMapping(value="/users",method=RequestMethod.GET)public String list(Model model) {model.addAttribute("users",users);return"user/list";}//链接到add页面时是GET请求@RequestMapping(value="/add",method=RequestMethod.GET)public String add(@ModelAttribute("user") User user) {return"user/add";}//具体添加用户时是post请求@RequestMapping(value="/add",method=RequestMethod.POST)public String add(@Validated User user,BindingResult br) {//一定要紧跟Validated之后写验证结果if(br.hasErrors()){//如果有错,就跳转到add视图return "user/add";}users.put(user.getUserName(), user);System.out.println("123");return"redirect:/user/users";}@RequestMapping(value="/{userName}/update" , method=RequestMethod.GET)public String update(@PathVariable String userName,Model model){model.addAttribute(users.get(userName));return "user/update";}@RequestMapping(value="/{userName}/update",method=RequestMethod.POST)public String update(@Validated User user,@PathVariable String userName,BindingResult br) {//一定要紧跟Validated之后写验证结果users.put(user.getUserName(), user);System.out.println("123");return"redirect:/user/users";}@RequestMapping(value="/{userName}/delete" , method=RequestMethod.GET)public String delete(@PathVariable String userName,Model model){users.remove(userName);return"redirect:/user/users";}@RequestMapping(value="/userlogin" , method=RequestMethod.POST)public String login ( String userName,String password,HttpSession session){//session.setAttribute('session', arg1)return"redirect:/user/users";}@RequestMapping(value="/{userName}" , method=RequestMethod.GET)public String show(@PathVariable String userName,Model model){System.out.println("userName="+userName);model.addAttribute(users.get(userName));return "user/show";}}login.jsp:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><form action="user/userlogin" method="post">userName<input type="text" name="userName"><br>password<input type="text" name="password"><br><input type="submit" name="提交"></form></body></html> 现在这里面的show方法和login 有一点冲突,因为我在地址栏输入的user/userlogin 它当作了show方法的userName参数了,有什么方法避免吗?
解决方案
因为我在地址栏输入的user/userlogin 它当作了show方法的userName参数了,有什么方法避免吗? 地址栏直接输入是get请求很明显 @RequestMapping(value="/userlogin" , method=RequestMethod.POST) 你的/userlogin只处理POST请求可以把method=RequestMethod.POST --->method={RequestMethod.GET, RequestMethod.POST}这样可以get/post了
解决方案二:
@RequestMapping(value="/userlogin" , method=RequestMethod.POST) public String login你是用POST方式提交的登陆而 @RequestMapping(value="/{userName}" , method=RequestMethod.GET) public String show你是用GET方式提交的当你在地址栏输入user/userlogin 的请求方式为GET 所以就到了show方法里避免的方式是 @RequestMapping(value="/方法名/参数1/参数2/..."