我正在學習struts2,並面臨一些問題,顯示addFieldError消息..沒有顯示出來.. 當我運行我的項目,並留下userID字段爲空它確實進入驗證方法,甚至滿足條件isEmpty方法,但它不顯示任何錯誤消息。 我把一個sop statment在的isEmpty方法看,它正在進入或不.. 的statment被印在控制檯中,URL變化../MyStruts/validation.action隨地沒有錯誤和沒有進一步發生..addFieldError在Struts2中不工作
能有人告訴我我在哪裏做錯了.. 這裏是我的代碼..
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="validate" extends="struts-default">
<action name="validation" class="com.staction.DataAction">
<result name="success">/success.jsp</result>
<result name="input">/form1.jsp</result>
<result name="error">/error.jsp</result>
</action> </package>
</struts>
form1.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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<s:form action="validation">
<h1>
<s:textfield label="UserName" name="userID" />
<s:password label="password" name="password" />
</h1>
<s:submit />
</s:form>
<s:a href="forget">forget password </s:a>
</html>
Action類...
package com.staction;
import org.apache.commons.lang3.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class DataAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = 1L;
User user;
DataCheck dc;
public DataAction() {
user=new User();
dc=new DataCheck();
}
@Override
public void validate() {
if(StringUtils.isEmpty(user.getUserID()))
{
System.out.println("hellp");
addFieldError("user.userID", "wtf!!!!");
}
}
public String execute()
{
System.out.println("user.userID");
/* if(dc.isValid(user))
{
return SUCCESS;
}*/
return ERROR;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
終於User
類..
package com.staction;
import org.apache.commons.lang3.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
public class User{
String userID;
String password;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在這裏得到了一些線索..如果我刪除modeldriven接口的實現,並把userData和密碼字段在myDataAction類,那麼它工作正常..有人可以告訴我爲什麼這樣嗎? ..請有人嘗試這個東西,並回復 – 2013-02-13 22:10:24