Spring MVC exception Handling and session additions

Source: Internet
Author: User

setting in Controller

 PackageCom.stone.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.validation.Valid;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;ImportOrg.springframework.validation.BindingResult;ImportOrg.springframework.web.bind.annotation.ExceptionHandler;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.ResponseBody;Importorg.springframework.web.bind.annotation.SessionAttributes;ImportOrg.springframework.web.servlet.view.InternalResourceViewResolver;ImportCom.stone.model.User, @Controller @requestmapping ("/USER") @SessionAttributes ("Loginuser")//session to be processed  Public classUsercontroller {Private Final Staticmap<string, user> users =NewHashmap<string, user>();  PublicUsercontroller () {Users.put ("LDH",NewUser ("LDH", "Andy Lau", "123", "[email protected]")); Users.put ("Zxy",NewUser ("Zxy", "Jacky", "123", "[email protected]")); Users.put ("GFC",NewUser ("GFC", "Aaron Kwok", "123", "[email protected]")); Users.put ("LM",NewUser ("LM", "Dawn", "123", "[email protected]")); } @RequestMapping ("/users")     PublicThe String list (model model) {Model.addattribute ("Users", users); return"User/list"; } @RequestMapping (Value= "/login", method =requestmethod.get) PublicString Login () {return"User/login"; } @RequestMapping (Value= "/login", method =requestmethod.post) Publicstring Login (string username, string password, model model) {if(!Users.containskey (username)) {            Throw NewRuntimeException ("User name does not exist! "); }        if(!password.equals (Users.get (username). GetPassword ())) {            Throw NewRuntimeException ("User password is incorrect"); }        //to set the sessionModel.addattribute ("Loginuser", Users.get (username)); return"Redirect:/user/users/"; }        //Perform exception handling@ExceptionHandler (value = {runtimeexception.class })     PublicString handlerexception (Exception ex, HttpServletRequest req) {Req.setattribute ("Ex", ex); return"Error"; }}

Spring MVC exception Handling and session additions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.