嗨,我对 struts2 很陌生。从一些书籍和网站我得到了这个例子。在动作类中,它使用 validate 方法来检查逻辑。该方法在 print 语句中称为 bcoz。但是该错误未显示在字段附近。请帮帮我
索引.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Home</title>
</head>
<body>
<s:form action="LoginAction ">
<s:textfield name="username" label="Username" />
<s:textfield name="password" label="Password" />
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
Struts.xml
<package name="default" extends="struts-default">
<action name="LoginAction" class="org.shammu.struts2.actions.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="emptypassthrough">
<result>index.jsp</result>
</action>
</package>
登录操作.java
package org.shammu.struts2.actions;
import org.shammu.struts2.beans.LoginBean;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{
private String username;
private String password;
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;
}
public String execute(){
LoginBean login = new LoginBean();
login.setPassword(password);
login.setUsername(username);
if (getUsername().equals("abcd")) {
return "success";
} else {
return "input";
}
}
@Override
public void validate() {
if (username.length() < 3) {
System.out.print("user err ok");
this.addFieldError(username, "Username Empty Pls provide");
}
if (password.length() < 3) {
System.out.print("Pass err ok");
this.addFieldError(password, "Password Empoty provride one pkls");
}
}
}
我不考虑执行方法。我希望将错误消息打印在字段附近...帮助