9. User module: User Login function

Source: Internet
Author: User

User Login Business:

1, user login page Click Login Verify user name and password can not be empty!

2, login Failure page does not jump, display login failure error message!

3, Login successfully jump to the first page, the first page shows the name of the person who logged in.


-------------------------Related Code-----------------------------------

1, login.jsp

First commit the execution function checkform the user name and password verification, and then submit to action= "${pagecontext.request.contextpath}/user_login.action", the code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib uri= "/struts-tags" prefix= "s" %><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "><!--saved from url= (0080) http://localhost:8080/mango/login.jhtml?redirectUrl=%2Fmango%2Fcart% 2flist.jhtml-->
2, according to the configuration in Struts.xml, you need to write the login method in Useraction.java. The code is as follows:

/** * Login * @return */public String Login () {User Existuser = Userservice.login (user); if (existuser==null) {// Login failed This.addactionmessage ("Login failed: User name or password is wrong or not activated! "); return LOGIN;} else{//login is successful and the information is stored in session servletactioncontext.getrequest (). GetSession (). SetAttribute ("Existuser", Existuser); return "Loginsuccess";}}

3. Action calls the login method in UserService,the Login method code in UserService is as follows:

/** * Login * @param user * @return */public User Login (user) {return userdao.login (user);

4, Userservice.java call Userdao in the login method, query user information needs to meet the user name password is correct and the status is 1 (active), the code is as follows:

/** * Login * @param user * @return */public User Login (user user) {String hql = "from user where username=? and password=? and state=? "; list<user> list = This.gethibernatetemplate (). Find (HQL, User.getusername (), User.getpassword (), 1); if (list!= Null && list.size () >0) {return list.get (0);} return null;}

5. The logical view returned in Useraction needs to be configured in Struts.xml with the following code:

<!--user Action--    <action name= "user_*" class= "useraction" method= "{1}" >    <result name= " Registpage ">/WEB-INF/jsp/regist.jsp</result>    <result name=" Input ">/web-inf/jsp/regist.jsp </result>    <result name= "msg" >/WEB-INF/jsp/msg.jsp</result>    <result name= "LoginPage" >/WEB-INF/jsp/login.jsp</result>    <result name= "Login" >/WEB-INF/jsp/login.jsp</result>    <result name= "loginsuccess" type= "redirect" >index</result>    </action>

6. When the login fails, the error message needs to be displayed in the login.jsp.

First, you need to introduce struts2 tags.

<%@ taglib uri= "/struts-tags" prefix= "s"%>

Then add the code that displays the error message to the page. <div><s:actionmessage/></div>

The login.jsp page code is listed above, which can be found in these two lines.


7, when the login is successful, you need to jump to the homepage, remove the login registration display, increase the display of the name of the login, my order and exit.

Because index.jsp contains menu.jsp information, struts2 tags need to be introduced in index.jsp , so menu.jsp can be used to make conditional judgments with struts2 tags.

The menu.jsp code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><div class= "Span10 Last" ><div class= "Topnav clearfix" ><ul><s:if test= "#session. Existuser = = null" ><li id= "Headerlogin" class= " Headerlogin "style=" Display:list-item; " ><a href= "${pagecontext.request.contextpath}/user_loginpage.action" > Login </a>|</li><li id= " Headerregister "class=" Headerregister "style=" Display:list-item; " ><a href= "${pagecontext.request.contextpath}/user_registpage" > Registration </a>|</li></s:if> <s:else><li id= "Headerlogin" class= "Headerlogin" style= "Display:list-item;" > Welcome: <s:property value= "#session. Existuser.name"/>|</li><li id= "Headerlogin" class= "HeaderLogin "Style=" Display:list-item; " ><a href= "#" > My order </a>|</li><li id= "Headerregister" class= "Headerregister" style= "Display: List-item; " ><a href= "#" > Exit </a>|</li></s:else><li><a> Member Center &LT;/A&GT;|</li><li><a> About Us </a></li></ul></div><div class= "cart" ><a href= "./shopping cart. htm" > Shopping cart </a></div><div class= "Phone" > Customer Service Hotline: <strong>88888888</strong ></div></div><div class= "Span24" ><ul class= "Mainnav" ><li><a href= "./index.htm "> Home </a>|</li><li><a href="./vegetables classification. htm "> Custom packages </a>|</li><li><a > Security Channel </a>|</li><li><a> Mall card </a>|</li><li><a> Vegetable base </a>|</ Li><li><a> Throttle Health </a>|</li><li><a> convenience service </a>|</li></ul> </div>

8.




9. User module: User Login function

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.