Spring MVC Learning Notes 8--implement simple user management (4) User Login

Source: Internet
Author: User
Tags exception handling stub throwable
Spring MVC Learning notes 8--implement simple user management (4) User Login

Check and delete, login

1. login.jsp, written on the outside, and with the web-inf of the same level of directory, such as: LS webcontent; >> Meta-inf Web-inf login.jsp

The form:<form> contains two parameters, action (target) & Method (Methods). The <input> label, type (text,password,submit), name, two parameters are used.

<form action = "User/login" method = "POST" >
   username:<input type= "text" name= "username"/><br/>< C2/>password:<input type= "password" name= "password"/><br/> <input type=
   "Submit"/>
</ Form>

2. Usercontroller.java

login.jsp clicks "Submit", will visit the login method in Usercontroller: public String Login () {}
· Specify criteria for entering method
· Specify parameters to enter method
· Exception handling 1: User name does not exist, exception handling 2: Incorrect password
· Save user to session ". 】

·

The @RequestMapping (value = "/login", method = Requestmethod.post)//requestmapping is the public String login that specifies how to enter this approach
	( String Username, string password, HttpSession session) {	//pass 2 arguments in, 1st:username 2nd:password
// The 3rd parameter is the
		if (!users.containskey (username)) {	//exception handling 1 specified session injection. If not, the username does not exist
			//Create an exception handler. Java, see next "3." A class "
			throw new Userexception (" Username does not exist! ") is required to handle the exception;
			User u = users.get (username);	Gets username
		if (!u.getpassword (). Equals (password))	{//if is not equal, stating that the password is incorrect:
			throw new Userexception (" Password does not match! ");
		}
		Save user to session:
		Session.setattribute ("Loginuser", u);
		return "Redirect:/user/users";	Login success, jump to User/users
	}

3. Need a class to handle the exception

·  Create a new class:
Name:UserException.class
Superclass:java.lang.RuntimExcption
·  Add Default Version ID: Code: Private static final long serialversionuid = 1L;

· Create a constructors constructor from superclass: Right key-resources-generate constructors from superclass

 

   Package Edu.bit.model;
        public class Userexception extends RuntimeException {public
        userexception () {
	        super ();
		TODO auto-generated constructor stub
	} public

	userexception (String arg0, Throwable arg1, Boolean arg2, Boolean ARG3) {
		super (arg0, Arg1, arg2, arg3);
		TODO auto-generated constructor stub
	} public

	userexception (String arg0, Throwable arg1) {
		super (arg0, ARG1);
		TODO auto-generated constructor stub
	} public

	userexception (String arg0) {
		super (arg0);
		TODO auto-generated constructor stub
	} public

	userexception (Throwable arg0) {
		super (arg0);
		TODO auto-generated constructor stub
	}

========== so far, the basic completion of a login operation, but if the user name does not exist or the password is incorrect, will return 500 error, and press Userexception ("error Prompt") Prompts the error ========

Test Link: http://localhost:8080/myhello/login.jsp
500 error Page Screenshot:



Implement simple user Management (4.2) User Login--Display local exception information Part Two: Display local exception information instead of 500 error page

1. Write a method to pass the userexception in.
2. Mapping method: @ExceptionHandler (), the value in brackets is Userexception.class
3. To enter the Userexception method: (userexception UE, HttpServletRequest req)
-----//1. Pass the userexception in;
-----//2. Can not use model to pass the value, because not requestmapping, with httpseverletrequest req
4. The exception object is deposited into the Httpseverletrequest req e parameter: Req.setattribute ("E", UE);
5. Return view Error:return error;

----Implementation Code-----
Usercontroller.java Add:

	
	 * * Display local exception information: Only can handle the exception in this controller
	 * Write a method, handlerexception, send userexception into the * * *
	@ExceptionHandler ( Value={userexception.class})	///Exceptionhandler to be mapped, the value to be processed is an array
							//To process an object so written, can handle multiple objects. Public
	String handlerexception (userexception UE, HttpServletRequest req) {	
				//1. Pass userexception in;
				2. Can not use model to pass the value, because is not requestmapping, with httpseverletrequest req
		//The exception object UE deposit in:
		req.setattribute ("E", UE);	Req "E" parameter, is set for uexception return
		"error";
	}

/web-inf/error.jsp code: ${e.message} FETCH message information:

<%@ 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" >




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.