springmvc中controller之间的跳转

刚开始学习springmvc,做了一个简单的登录功能


<form action="user/controller/login" method="post">


之后来到后台


@Controller
@RequestMapping("/user/controller")
public class UserController extends BaseController  {
	
	@Autowired
	protected UserService userService;
	
	@RequestMapping("/home")
	public String Home() {
		return "user/mainPage";
	}
	
	@RequestMapping(value="/login")
	public String login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("im in login class!!!");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = userService.findByUserName(username);
		if(password.equals(user.getPassword())) {
			request.getSession().setAttribute("user", user);
			request.setAttribute("info", "login successfully");
			return "redirect:/user/controller/home";
		}
		else {
			request.setAttribute("info", "password error!");
			return "../../login";
		}
	}

}

这时候通过login()我们跳转到了Home(),这时候实现了controller之间的跳转,这样登录进入到主页的时候url就不会是.....user/controller/login而是...user/controller/home了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章