3

我想将 3 个按钮添加到 struts 表单中。我的代码是:

@Action("/admin/product/insert")
public String insert() throws Exception {
    if(upload.hasFile()){
        model.setImage(upload.getFileFileName());
        upload.saveTo("../images/customers/");
    }
    else{
        model.setImage("product.png");
    }
    XHibernate.save(model);
    return "new";
}

@Action("/admin/product/update")
public String update() throws Exception {
    if(upload.hasFile()){
        model.setImage(upload.getFileFileName());
        upload.saveTo("../images/customers/");
    }
    System.out.println("update");
    XHibernate.update(model);
    return "input";
}

@Action("/admin/product/delete")
public String delete() throws Exception {
    XHibernate.delete(model);
    return "new";
}

在jsp中:

<s:form enctype="multipart/form-data" theme="simple" role="form">

    <div class="form-group col-md-4">
        <label for="id">Product Id</label>
        <s:textfield id="productId" name="id" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="name">Product Name</label>
        <s:textfield name="name" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="unitPrice">Unit Price</label>
        <s:textfield name="unitPrice" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="unitBrief">Unit Description</label>
        <s:textfield name="unitBrief" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="discount">Discount</label>
        <s:textfield name="discount" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="quantity">Quantity</label>
        <s:textfield name="quantity" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="productDate">Product Date</label>
        <s:textfield name="productDate" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="supplier.id">Supplier</label>
        <s:select name="supplier.id" list="suppliers" listKey="id"
            listValue="name" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label for="category.id">Category</label>
        <s:select name="category.id" list="categories" listKey="id"
            listValue="name" cssClass="form-control" />
    </div>

    <div class="form-group col-md-4">
        <label>Features</label>
        <div>
            <label><s:checkbox name="available" /> Available</label> <label><s:checkbox
                    name="special" /> Special</label> <label><s:checkbox
                    name="latest" /> latest</label>
        </div>
    </div>

    <div class="form-group col-md-4">
        <label for="image">Image</label> <input id="image" type="file"
            name="upload.file">
        <s:hidden name="image" />
    </div>

    <div class="form-group col-md-12">
        <label for="description">Description</label>
        <s:textarea id="index_description" name="description" rows="30"
            cssClass="form-control" />
    </div>

    <div class="col-md-12">
        <s:submit value="Thêm mới" action="admin/product/insert"
            cssClass="btn btn-default" />
        <s:submit value="Cập nhật" action="admin/product/update"
            cssClass="btn btn-default" />
        <s:submit value="Xóa" action="admin/product/delete" cssClass="btn btn-default" />
        <a href="product/index" class="btn btn-default">Nhập lại</a>
    </div>
</s:form>

问题是它不能调用确切的动作。

我总是收到错误消息:“没有为命名空间 /admin/product 和操作名称 admin/product/update 映射操作。”。

我必须将其拆分为 3 种形式吗?或者有没有办法像我的代码一样以 1 种形式为 3 个动作提供 3 个按钮?任何帮助都会很棒。

启用 devmode 时更新了日志记录:

ognl.MethodFailedException: Method "setDiscount" failed for object eshop.entity.Product@48e4d3df [java.lang.NoSuchMethodException: setDiscount([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:50)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:155)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:143)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:273)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
4

1 回答 1

2

要解决您当前的问题,您应该添加

<constant name="struts.enable.SlashesInActionNames" value="true"/>

在 struts.xml 中,默认为 false,并且会将最后一个斜杠之前的所有部分作为命名空间而不是操作名称。顺便说一句,这有副作用:

带有斜杠的动作名称

如果您的操作名称中包含斜杠(例如,
<action name="admin/home" class="tutorial.Admin"/>),您需要通过 struts.xml 文件中的常量明确允许您的操作名称中包含斜杠,方法是指定

<constant name="struts.enable.SlashesInActionNames" value="true"/>.

请参阅JIRA 问题 WW-1383进行讨论,因为将此属性设置为 true 会有副作用

以正确的方式解决问题,您需要正确使用命名空间:

行动

@Namespace("/admin/product")
public class YourAction extends ActionSupport

    @Action("insert")
    public String insert() throws Exception {
        /* .... */
    }

    @Action("update")
    public String update() throws Exception {
        /* .... */
    }

    @Action("delete")
    public String delete() throws Exception {
        /* .... */
    }

JSP

<s:submit value="Thêm mới" action="insert" cssClass="btn btn-default" />
<s:submit value="Cập nhật" action="update" cssClass="btn btn-default" />
<s:submit value="Xóa"      action="delete" cssClass="btn btn-default" />

您还应该重构 Actions 和 JSP 的包结构以遵循命名空间结构。

PS:如果您使用高于2.3.15.2的 Struts2 版本,请记住按照此答案中的说明设置操作前缀:

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

编辑

如果namespace您用来显示JSPAction的/admin/product<s:form>

<s:form namespace = "/admin/product" 
          enctype = "multipart/form-data" 
            theme = "simple" 
             role = "form">

并且(如果您使用高于2.3.15.2的 Struts2 版本),请在 struts.xml 中设置以下属性:

<constant name="struts.mapper.action.prefix.crossNamespaces" value="true"/>

因为出于安全原因,它已默认禁用。

于 2014-12-01T09:23:45.957 回答