First, let’s post the matching environment:
Configuration:
Eclipse4.3.2
jdk1.7_45
Mysql 5.0+
Then get to the point:
1. login.jsp
Mainly, you can use the OGNL tag or use the html form form to call LoginAction.action, and transmit it in post.
After judgment in LoginaAction, there will be a prompt message, which needs to be displayed using <s:fielderror/>.
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Login</title> </head> <body> <center> Please log in<!-- this.addActionError( "Username or password is incorrect!"); Action The display information set in the html should reference the OGNL expression --> <s:actionerror/> <%-- <s:fielderror/> --%> <s:form action="LoginAction.action" method="post"> <s:label value="Username:" /> <s:textfield name="userName" /> <br /> <s:label value="password" /> <s:textfield name="userPwd" /> <br /> <s:submit value="LoginAction" /> </s:form> </center> </body>
2. struts.xml
Configuration <br />The namespace is "/", inherit "struts-default"
Login successfully, jump to index.jsp
Login failed, login.jsp will be returned
<pre name="code"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- The Struts2 OGNL tag is used by default. Struts2 will be processed and will be more formatted. If you do not refer to it, you can add this property --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <action name="LoginAction"> <result name="success">/index.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
3. LoginAction.java
LoginAction
Inherit the ActionSupport method and override the execute() and validate() methods:
execute method call the value of the database called from the background
The validate method is used to determine whether the user name and password input is empty, and remind you that the method is called in login.jsp. You can call the setting information directly by default without setting it, unless you set it in strtus.xml.
package com.tikitoo.action; import com.opensymphony.xwork2.ActionSupport; import com.tikitoo.service.UserInfoService; import com.tikitoo.service.UserInfoServiceImpl; /** * @author Tikitoo1 * @see com.opensymphony.xwork2.ActionSupport * @see com.opensymphony.xwork2.ActionSupport * */ public class LoginAction extends ActionSupport { private static final long serialVersionUID = -4760561602154545441L; /** * Struts2 default call method* @return Struts2 result Return value*/ @Override public String execute() throws Exception { UserInfoService userInfoService = new UserInfoServiceImpl(); boolean flag = userInfoService.loginByUserNameAndUserPwd( userName, userPwd); String msg = ""; if ( flag == true) { this.addFieldError( "true", "Login successfully"); msg = "success"; } else { this.addFieldnError( "Username or password is incorrect!"); msg = "input"; } return msg; }// execute() end /** * Login verification* Rewrite ActionSupport method*/ @Override public void validate() { // Determine whether the user name is empty if ( getUserName() == null || "".equals( getUserName().trim() ) ) { this.addFieldError( "userName", "Username cannot be empty"); } // Determine whether the password is empty if ( getUserPwd() == null || "".equals( getUserPwd().trim() )) { this.addFieldError("userPwd", "Password cannot be empty"); } }// validate() end private String tip; public String getTip() { return tip; } private String userName; private String userPwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }The username and password are entered incorrectly:
If the user name is entered correctly, the login will be successful:
The above is all about this article, I hope it will be helpful to everyone's learning.