如何在 Struts 2 中使用包含上下文属性和 <s:a> 标签?

问题描述 投票:0回答:1

根据文档,我可以使用

includeContext = "false"
来避免实际上下文应包含在 URL 中。

当我使用这条线时:

<s:a action="persist" namespace="books" includeParams="get" includeContext="false" cssClass="btn btn-xs btn-success">
   <s:text name="actions.books.edit" />
   <s:param name="idBook"><s:property value="id" /></s:param>
</s:a>

生成的网址是这样的:

http://localhost:8080/ejercicio4-web/books/books/persist.action?idBook=6

但是当前的网址:

http://localhost:8080/ejercicio4-web/books/all.action

如果我使用

<s:url>
组件(如果它工作正常):

http://localhost:8080/ejercicio4-web/books/persist.action?idBook=6

struts.xml
完整配置:

<!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.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true"/>
    <!-- Use Guice as the ObjectFactory for this application -->
    <constant name="struts.objectFactory" value="guice" />
    <constant name="struts.custom.i18n.resources" value="global" />
    <constant name="struts.url.includeParams" value="get" />
    <!-- Default App Package -->
    <package name="app-default" abstract="true" extends="struts-default, tiles-default">
        <interceptors>
            <interceptor name="saveParamsOnSession" class="interceptors.SaveParamsOnSession"/>
            <interceptor-stack name="secure">
                <interceptor-ref name="roles">
                    <param name="allowedRoles">ADMIN</param>
                </interceptor-ref>
                <interceptor-ref name="paramsPrepareParamsStack"/>
            </interceptor-stack>
            <interceptor-stack name="storeFlashDataStack">
                <interceptor-ref name="store">
                    <param name="operationMode">STORE</param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
            <interceptor-stack name="storeParamsPrepareParamsStack">
                <interceptor-ref name="store">
                    <param name="operationMode">STORE</param>
                </interceptor-ref>
                <interceptor-ref name="paramsPrepareParamsStack" />
                <interceptor-ref name="saveParamsOnSession" />
            </interceptor-stack>
        </interceptors>
        <global-results>
            <result name="ejbexception" type="tiles">app.error.ejbexception</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="javax.ejb.EJBException" result="ejbexception" />
        </global-exception-mappings>
    </package>
    <!-- Books package -->
    <package name="books" namespace="/books" extends="app-default">
        <!--<default-interceptor-ref name="secure"/>-->
        <default-action-ref name="home" />
        <action name="home" class="actions.BaseAction">
            <result type="tiles">app.books.home</result>
        </action>
        <!-- persist book -->
        <action name="persist" class="actions.books.PersistBookAction">
            <interceptor-ref name="store">
                <param name="operationMode">RETRIEVE</param>
            </interceptor-ref>
            <interceptor-ref name="servletConfig" />
            <interceptor-ref name="params" />
            <interceptor-ref name="prepare" />
            <result type="tiles">app.books.persist</result>
        </action>
        <action name="proccess" class="actions.books.PersistBookAction" method="proccess">
            <interceptor-ref name="storeParamsPrepareParamsStack" />
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">application/pdf</param>
            </interceptor-ref>
            <exception-mapping exception="java.io.IOException" result="input" />
            <result name="input" type="redirectAction">persist.action?idBook=${book.id}</result>
            <result type="redirectAction">persist.action?idBook=${book.id}</result>
        </action>
        <!-- Show Book -->
        <action name="show" class="actions.books.BookAction">
            <interceptor-ref name="paramsPrepareParamsStack" />
            <result type="tiles">app.books.show</result>
        </action>
        <action name="pdf" class="actions.books.ShowPDFAction">
            <interceptor-ref name="basicStack" />
            <result name="success" type="stream">
                <param name="contentType">application/pdf</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">inline;filename="test.pdf"</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>
        <!-- Remove book -->
        <action name="remove" class="actions.books.RemoveBookAction">
            <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
            </interceptor-ref>
            <interceptor-ref name="paramsPrepareParamsStack" />
            <result name="input" type="redirectAction">all</result>
            <result type="redirectAction">all</result>
        </action>
        <!-- Books list -->
        <action name="all" class="actions.books.BooksAction">
            <interceptor-ref name="storeParamsPrepareParamsStack" />
            <result type="tiles">app.books.all</result>
        </action>
    </package>
     <!-- Authors package -->
    <package name="authors" namespace="/authors" extends="app-default">
        <action name="create" class="actions.authors.CreateAuthorAction">
            <interceptor-ref name="storeFlashDataStack" />
            <result name="input" type="redirectAction">books/persist</result>
            <result type="redirectAction">
                <param name="actionName">persist</param>
                <param name="namespace">/books</param>
            </result>
        </action>
    </package>
    <!-- Accounts package -->
    <package name="accounts" namespace="/accounts" extends="app-default">
        <action name="login" class="actions.accounts.SigninAction">
            <interceptor-ref name="store">
                <param name="operationMode">RETRIEVE</param>
            </interceptor-ref>
            <result>/WEB-INF/views/accounts/login.jsp</result>
        </action>
        <action name="check" class="actions.accounts.SigninAction" method="check">
            <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
            </interceptor-ref>
            <result name="input" type="redirectAction">login</result>
            <result type="redirectAction">login</result>
        </action>
        <action name="signup" class="actions.accounts.PreCreateUserAction">
            <interceptor-ref name="store">
                <param name="operationMode">RETRIEVE</param>
            </interceptor-ref>
            <result type="tiles">app.accounts.signup</result>
            <result name="input" type="tiles">app.accounts.signup</result>
        </action>
        <action name="create" class="actions.accounts.PostCreateUserAction">
            <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
            </interceptor-ref>
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/jpeg,image/jpg,image/png</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />
            <result name="input" type="redirectAction">signup</result>
            <result type="redirectAction">signup</result>
        </action>
    </package>
</struts>
jsp url struts2 struts-tags
1个回答
1
投票

为什么使用

includeContext
属性?你应该删除它

<s:a action="persist" namespace="books" cssClass="btn btn-xs btn-success">
   <s:text name="actions.books.edit" />
   <s:param name="idBook"><s:property value="id" /></s:param>
</s:a>

框架使用

UrlHelper
为所有标签生成 url,因此它们无法预测您如何使用生成的代码。
includeContext
Boolean
属性,表示

“URL 中是否应包含实际上下文”


描述

创建 HTML 的标签。该标签支持相同的属性 作为“url”标签,包括使用“param”标签的嵌套参数。

还有你为什么使用

includeParams="get"
? url 中只有一个参数。

© www.soinside.com 2019 - 2024. All rights reserved.