struts.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="getLogin" class="login.action.LoginAction"
method="login">
<result name="success">/Profile.jsp</result>
<result name="input">/LoginError.jsp</result>
</action>
<action name="getRegistered" class="login.action.LoginAction"
method="register">
<result name="success">/Success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
我试图将此XML中的两个Action映射到Action类
中的两个不同方法LoginAction.java:
package login.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import login.service.LoginDao;
import login.service.RegisterDao;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
private String username;
private String password;
private HttpServletRequest httpServletRequest;
private String firstname;
private String lastname;
private String regpassword;
private String conpassword;
private String regemail;
private String conemail;
private String phone;
public String register(){
RegisterDao rdao = new RegisterDao();
String registered = rdao.registerdao(firstname,lastname,regpassword,conpassword,regemail,conemail,phone);
if(registered == "TRUE"){
return SUCCESS;
}
return INPUT;
}
public String login() {
System.out.println("user name::: "+username);
httpServletRequest.getSession().setAttribute("key", username);
httpServletRequest.getSession().setAttribute("key", password);
LoginDao db = new LoginDao();
Boolean validate = db.loginresult(username, password);
if (validate == true) {
return SUCCESS;
} else {
return INPUT;
}
}
public HttpServletRequest getHttpServletRequest() {
return httpServletRequest;
}
public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
this.httpServletRequest = httpServletRequest;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getRegpassword() {
return regpassword;
}
public void setRegpassword(String regpassword) {
this.regpassword = regpassword;
}
public String getConpassword() {
return conpassword;
}
public void setConpassword(String conpassword) {
this.conpassword = conpassword;
}
public String getRegemail() {
return regemail;
}
public void setRegemail(String regemail) {
this.regemail = regemail;
}
public String getConemail() {
return conemail;
}
public void setConemail(String conemail) {
this.conemail = conemail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setServletRequest(HttpServletRequest request) {
this.httpServletRequest = request;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
}
}
使用登录和注册方法的Action类:
**login.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="getLogin.action" method="post" validate="true">
<s:textfield label="Name" key="username"/>
<s:password label="Password" key="password"/>
<s:checkbox name="Rememberme" label="Remember Me" value="yes"
checked="checked"/>
<s:submit value="Sign in"/>
</s:form>
</center>
</body>
</html>
Login.jsp,它重定向到Action类中的login方法。早些时候它工作正常。现在这个动作给出了麻烦:
**Registration.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="getRegistered.action" method="post" validate="true">
<div>
<table>
<s:textfield label="First Name" key="firstname" />
<s:textfield label="Last Name" key="lastname" />
<s:password label="Create your password" key="regpassword" />
<s:password label="Confirm your password" key="conpassword" />
<s:textfield label="Email" key="regemail1" />
<s:textfield label="Re-Type Email" key="conemail" />
<s:textfield label="Phone" key="phone" />
<tr>
<td><s:submit value="Register" theme="simple"/></td>
<td><s:submit value="Cancel" theme="simple" onclick="document.forms[0].action='login.jsp';" /></td>
</tr>
</table>
</div>
</s:form>
</center>
</body>
</html>
Registration.jsp重定向到getRegistered操作:
**LoginAction-Validation.xml**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<!-- Login page validation Starts -->
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>User Name is mandatory</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>User Name must be between 5 to 10 characters long</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Enter your password</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>Password must be between 5 to 10 characters long</message>
</field-validator>
<field-validator type="regex">
<param name="expression">((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{5,10})</param>
<message>Password must </message>
</field-validator>
</field>
<!-- Login page validation ends -->
</validators>
验证xml登录用户名和密码..
当我运行此代码时,它始终返回&#34; INPUT&#34;在登录操作的情况下字符串并重定向到LoginError.jsp,在寄存器操作的情况下重定向到login.jsp。请建议我如何在不使用validation.excludeMethods
的情况下解决此问题的LoginAction-getRegistered-validation.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<!-- Registration validation Starts -->
<field name="firstname">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Firstname is mandatory</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>First Name must be between 5 to 10 characters long</message>
</field-validator>
</field>
<field name="lastname">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Lastname is mandatory</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>Last Name must be between 5 to 10 characters long</message>
</field-validator>
</field>
<!-- Registration validation ends -->
</validators>
我已对第一个和最后一个名称进行了验证
答案 0 :(得分:0)
从.action
中的操作名称中删除<s:form>
扩展名,并确保在部署时将{mm}复制到WEB-INF/classes
下。
<s:form action="getRegistered" method="post" validate="true">
更简洁的方法是使用<s:url>
并在<s:form>
中引用创建的网址。
<s:url var="actionUrl" action="getRegistered" namespace="" />
<s:form action="%{actionUrl}" method="post" validate="true">
答案 1 :(得分:0)
在每个类的验证文件中,因此它验证此类中由validation
拦截器拦截的所有操作/方法。您应该为第一个操作getLogin
(例如LoginAction-getLogin-validation.xml
)分隔验证逻辑,并为getRegistered
操作定义另一个验证文件,例如LoginAction-getRegistered-validation.xml
。它们将按行动处理。此外,regex
验证程序的参数为regex
,而不是expression
。
<field-validator type="regex">
<param name="regex"><![CDATA[((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{5,10})]]></param>
<message>Password must </message>
</field-validator>