我认为我的问题非常简单,但我正在尝试一周后才能完成这项工作。
我有一个MainAction
,其中包含Processes
列表,如下所示:
private List<Process> processes;
使用getter和setter
编辑或删除我有processForm.jsp和ProcessAction
的流程。
所以我的问题是从列表中获取特殊进程的id到ProcessAction
。
当我尝试这个时:
<s:hidden name="processes[%{#status.index}].id"/>
id在隐藏字段
中<input type="hidden" name="processes[0].id" value="20" id="ShowMain_processes_0__id">
但未映射到操作
当我这样尝试时:
<s:hidden name="process.id" value="processes[%{#status.index}].id"/>
根本不起作用....
有人帮我怎么办?这里有新的细节:
struts.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="tiles-default">
<action name="ShowMain" class="com.goemon.smart.action.MainAction">
<result type="tiles">mainForm</result>
</action>
<action name="Cancel" class="com.goemon.smart.action.MainAction">
<result type="redirectAction">ShowMain</result>
</action>
<action name="ExecuteStartAction" class="com.goemon.smart.action.MainAction" method="executeStartCommand">
<result name="success" type="redirectAction">ShowMain</result>
<result name="input" type="redirectAction">ShowMain</result>
</action>
<action name="ExecuteStopAction" class="com.goemon.smart.action.MainAction" method="executeStopCommand">
<result name="success" type="redirectAction">ShowMain</result>
<result name="input" type="redirectAction">ShowMain</result>
</action>
<action name="DeleteProcess" class="com.goemon.smart.action.MainAction" method="deleteProcess">
<result name="success" type="redirectAction">ShowMain</result>
</action>
<action name="ShowAction" class="com.goemon.smart.action.ProcessAction">
<result type="tiles">actionForm</result>
</action>
<action name="CreateAction" class="com.goemon.smart.action.ProcessAction" method="createAction">
<result name="success" type="redirectAction">ShowMain</result>
<result name="error" type="redirectAction">ShowAction</result>
<result name="input" type="redirectAction">ShowAction</result>
</action>
<action name="EditCommandAction" class="com.goemon.smart.action.ProcessAction" method="editAction">
<result name="success" type="tiles">actionForm</result>
</action>
<action name="InsertDefault" class="com.goemon.smart.action.ProcessAction" method="insertDefaults">
<result name="success" type="redirectAction">ShowMain</result>
</action>
</package>
mainForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<div class="container">
<legend>
<h1><s:text name="header.mainpage"/></h1>
</legend>
<s:iterator var="currentProcess" value="processes">
<div class="jumbotron well col-lg-5 col-lg-offset-1">
<s:form theme="bootstrap" cssClass="bs-example form-horizontal">
<div class="btn-toolbar pull-right">
<s:a action="DeleteProcess">
<button type="button" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
</s:a>
<s:a action="EditCommandAction">
<button type="button" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
</button>
</s:a>
</div>
<s:hidden name="id" value="%{#currentProcess.id}"/>
<h2>
<s:text name="%{#currentProcess.name}"/>
</h2>
<div class="btn-toolbar pull-right">
<s:submit key="btn.start" action="ExecuteStartAction"
cssClass="btn btn-primary btn-lg btn-success"/>
<s:submit key="btn.stop" action="ExecuteStopAction"
cssClass="btn btn-primary btn-lg btn-danger"/>
</div>
</s:form>
</div>
</s:iterator>
<s:debug/>
processForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<div class="col-lg-6 col-lg-offset-3 well">
<div class="col-lg-12">
<s:form theme="bootstrap" cssClass="bs-example form-horizontal">
<legend>
<s:text name="header.manage"/>
</legend>
<s:hidden name="initial"/>
<s:textfield name="process.name" key="lbl.process.name" cssClass="col-lg-12"/>
<s:textfield name="process.host" key="lbl.process.host" cssClass="col-lg-12"/>
<div class="col-lg-11 pull-right">
<s:iterator var="counter" begin="0" end="2">
<div class="col-lg-12">
<s:hidden name="process.commands[%{#counter}].id"/>
<s:select key="lbl.command.type" list="commandTypeValues"
name="process.commands[%{#counter}].commandType" headerKey=""
headerValue="%{commandTypeValues[#counter]}"/>
<s:textfield name="process.commands[%{#counter}].command"
key="lbl.process.command"/>
</div>
</s:iterator>
</div>
<s:textfield name="process.user" key="lbl.process.user" cssClass="col-lg-12"/>
<s:textfield name="process.password" key="lbl.process.password" cssClass="col-lg-12"/>
<s:submit key="btn.create" action="CreateAction" cssClass="btn btn-primary pull-right"/>
<s:submit key="btn.cancel" action="Cancel" cssClass="btn btn-primary pull-right"/>
<s:debug />
</s:form>
</div>
ProcessAction.java
package com.goemon.smart.action;
import com.goemon.smart.model.CommandType;
import com.goemon.smart.model.Process;
import com.goemon.smart.service.ProcessService;
import com.goemon.smart.util.DefaultingUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Created by John on 4/4/15.
*/
public class ProcessAction extends ActionSupport implements Preparable {
@Autowired
private ProcessService processService;
@Autowired
private DefaultingUtil defaultingUtil;
private Process process;
private Long id;
private CommandType[] commandTypeValues = CommandType.values();
@Override
public void prepare() throws Exception {
}
public String execute() {
return SUCCESS;
}
public String createAction() {
try {
processService.saveAction(process);
} catch (Exception e) {
return INPUT;
}
return SUCCESS;
}
public String editAction() {
return SUCCESS;
}
public String insertDefaults() {
try {
Process ambiLight = defaultingUtil.ambiLightDefault();
processService.saveAction(ambiLight);
} catch (Exception e) {
return SUCCESS;
}
return SUCCESS;
}
public Process getProcess() {
return process;
}
public void setProcess(Process process) {
this.process = process;
}
public CommandType[] getCommandTypeValues() {
return commandTypeValues;
}
public void setCommandTypeValues(CommandType[] commandTypeValues) {
this.commandTypeValues = commandTypeValues;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
答案 0 :(得分:2)
迭代时,您不需要状态对象来访问字段。 Struts将当前列表项放在值堆栈的顶部,这样就足够了:
<s:hidden name="process.id" value="%{id}"/>
或者,您可以在迭代器标记中指定变量名称:
<s:iterator var="currentProcess" value="processes">
<s:hidden name="process.id" value="%{#currentProcess.id}"/>
</s:iterator>
请注意,name
属性必须与ProcessAction
中的字段匹配。因此,如果name="process.id"
需要字段process
,则必须初始化类型Process
,在Struts插入值之前,您将获得NullpointerException。
如果您使用<s:hidden/>
,请确保使用<s:submit>
标记,就像您为&#34; ExecuteStartAction&#34;而不是<button>
。
如果您想使用链接而不是表单按钮,请使用:
<s:url var="editUrl" action="EditCommandAction">
<s:param name="id" value="%{#currentProcess.id}" />
</s:url>
<s:a href="%{editUrl}">...</s:a>
然后,您可以使用CSS来设置您想要的链接样式。
但正如亚历山大M已经说过的那样,不要混合按钮和链接。