2013-03-15 16 views
1

我使用Struts2的校驗框架,我只是想驗證測試()方法TestAction類,這裏是我的代碼和配置:Struts2的校驗框架只是打印錯誤信息到控制檯,而不是重定向到輸入JSP

@Namespace(value = "/") 
@Result(name="input", location="test_input.jsp") 
public class TestAction 
{ 
private String name; 

public String getName() 
{ 
    return name; 
} 

public void setName(String name) 
{ 
    this.name = name; 
} 

@Action(value = "test", results = { @Result(name = "success", location = "test.jsp")}) 
public String test() throws Exception 
{ 
    return "success"; 
} 

@Action(value = "show", results = { @Result(name = "success", location = "test_input.jsp")}) 
public String show() throws Exception 
{ 
    return "success"; 
} 
} 

test_input.jsp:

<body> 
<s:fielderror></s:fielderror> 
<s:form action="test.do" theme="xhtml"> 
    <s:textfield name="name" label="name"></s:textfield> 
    <s:submit value="submit"></s:submit> 
</s:form> 
</body> 

test.jsp的:

<body> 
success 
<s:debug></s:debug> 
</body> 

TestAction-TE st-validation.xml:

<validators> 
<field name="name"> 
    <field-validator type="requiredstring"> 
     <message>name is required.</message> 
    </field-validator> 
</field> 
</validators> 

TestAction.class和TestAction-test-validation.xml位於同一個包中。

的struts.xml:

<struts> 

<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
<constant name="struts.devMode" value="true" /> 
<constant name="struts.ui.theme" value="simple" /> 
<constant name="struts.ognl.allowStaticMethodAccess" value="true" /> 

<constant name="struts.action.extension" value="do" /> 
<constant name="struts.objectFactory" value="spring" /> 
<constant name="struts.convention.result.path" value="/" /> 


<include file="struts-default.xml"></include> 

<package name="default" namespace="/" extends="struts-default"> 
    <default-interceptor-ref name="defaultStack"></default-interceptor-ref> 
    <global-results> 
     <result name="error">error.jsp</result> 
    </global-results> 

    <global-exception-mappings> 
     <exception-mapping exception="java.lang.Exception" 
      result="error" /> 
    </global-exception-mappings> 

</package> 
</struts> 

的web.xml:

<filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>*.do</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
</filter-mapping> 

我開始通過瀏覽器訪問show.do,輸入和按鈕出現,我不輸入任何輸入,只需要直接點擊按鈕,頁面會顯示test.do並在頁面中顯示'success',然後查看'debug'內容,其中沒有任何錯誤消息。 但奇怪的是: [錯誤] name:name的驗證錯誤是必需的。 上面的消息是在eclipse控制檯打印的(我在eclipse中啓動tomcat) 這個錯誤信息的意思是TestAction-test-validation.xml必須由struts2執行,但爲什麼不重定向到test_input.jsp?我已經定義了一個輸入結果。

我的配置有什麼問題或者我缺少其他配置嗎?

有什麼想法?

+0

它做同樣的事情,如果你從你的S2的配置文件中刪除了「簡單」的主題不變? – 2013-03-15 15:04:59

+0

@DaveNewton,是的。 – hiway 2013-03-16 06:10:25

回答

1

嘗試從您的操作中延伸ActionSupport

public class TestAction extends ActionSupport { 

它總是一個好主意,包括它,通常沒有理由不擴展在你的項目的每一個動作。

P.S:這種方式,您可以返回SUCCESS代替"success"

+0

好吧,它的工作原理,但我仍然不明白爲什麼我必須擴展ActionSupport。 – hiway 2013-03-16 06:11:34

+0

@HiwayChe因爲那是驗證意識到功能的來源,如果你沒有自己實現。 – 2013-03-16 12:02:12

相關問題