我写了一个类UserController:
[code="java"]
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";
}
}
[/code]
login.jsp:
[code]
<%@ 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">
Insert title here
userName
password
[/code]
现在这里面的show方法和login 有一点冲突,因为我在地址栏输入的user/userlogin 它当作了show方法的userName参数了,有什么方法避免吗?