我的Struts xml文件有什么问题?

时间:2013-10-29 07:38:26

标签: jquery json struts2

我将在Struts 2中通过jQuery AJAX提交表单。

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>

<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="default" extends="struts-default">

    <action name="loginSubmit"
        class="com.myproj.portal.controllers.PhysicianLoginController"
        method="execute">
        <result name="success">/landing.jsp</result>
        <result name="failure">/staff-login.jsp</result>
    </action>
    <action name="logout"
        class="com.myproj.portal.controllers.PhysicianLoginController"
        method="logout">
        <result name="success">/Welcome.htm</result>
    </action>
    <action name="demographics"
        class="com.myproj.portal.controllers.DemographicsController"
        method="execute">
        <interceptor-ref name="token" />
        <interceptor-ref name="basicStack" />

        <result name="success">/demographics.jsp</result>
        <result name="failure">/demographics.jsp</result>
        <result name="invalid.token">/demographics.jsp</result>
    </action>       
</package>

<package name="example" extends="struts-default,json-default">  
    <action name="chngpass"  class="com.myproj.portal.controllers.ChangePasswordController"
    method="execute" >
         <result name="success" type="json" />
    </action>
</package>  
  </struts>

我在我的lib文件夹中添加了JSON插件jar。但这会引发以下错误

12:40:41,236 ERROR [DomHelper] The content of element type "package" must match "(result-types?,interceptors?,default-intercept
org.xml.sax.SAXParseException: The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)

它有什么问题吗?

1 个答案:

答案 0 :(得分:0)

要获得json的结果类型,您无需定义以下内容

<result-types>
    <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>

<interceptors>
    <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
</interceptors>

删除上述声明。

其次,您的包声明应该在struts元素中。所以最后,你应该有这样的东西

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="example" namespace="/your_name_space" extends="struts-default,json-default">  
    <action name="chngpass"  class="com.myproj.portal.controllers.ChangePasswordController"
    method="execute" >
         <result name="success" type="json" />
    </action>
</package>
</struts>