我正在从数据库中填充<s:select>
。动作类是模型驱动的。
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class TestAction extends ActionSupport implements Serializable, ValidationAware, Preparable, ModelDriven<Transporter>
{
@Autowired
private final transient SharableService sharableService=null;
private static final long serialVersionUID = 1L;
private Transporter transporter; //Getter and setter
private Long transporterId; //Getter and setter.
private List<Transporter> transporters; //Getter only.
@Action(value = "Test",
results = {
@Result(name=ActionSupport.SUCCESS, location="Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs={@InterceptorRef(value="defaultStack", params={"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String load() throws Exception
{
return ActionSupport.SUCCESS;
}
@Validations(
requiredFields={@RequiredFieldValidator(fieldName="transporterId", type= ValidatorType.FIELD, key = "transporter.required")})
@Action(value = "testInsert",
results = {
@Result(name=ActionSupport.SUCCESS, location="Test.jsp", params={"namespace", "/admin_side", "actionName", "Test"}),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs={@InterceptorRef(value="defaultStack", params={"validation.validateAnnotatedMethodOnly", "true"})})
public String insert() {
System.out.println("Selected item in the drop box : "+transporterId);
return ActionSupport.SUCCESS;
}
@Override
public void prepare() throws Exception {
transporters=sharableService.getTransporterList();
}
@Override
public Transporter getModel() {
return transporter;
}
}
以下是<s:select>
:
<s:select id="transporterId"
name="transporterId"
list="transporters"
value="transporterId"
listKey="transporterId"
listValue="transporterName"
headerKey="" headerValue="Select"
listTitle="transporterName"/>
这很有效。
我需要在另一个实现<s:select>
的动作类中使用此ModelDriven<ZoneTable>
。
表结构很简单,transporter->zone_table->country->state->city
。这些表之间存在一对多的关系。
我们如何才能有一个模型驱动的动作类实现ModelDrven<ZoneTable>
,其中Transporter
可以映射到<s:select>
,类似于什么?
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class ZoneAction extends ActionSupport implements Serializable, ValidationAware, Preparable, ModelDriven<ZoneTable>
{
@Autowired
private final transient ZoneService zoneService=null;
@Autowired
private final transient SharableService sharableService=null;
private ZoneTable entity=new ZoneTable(); //Getter and setter.
private Long transporterId; //Getter and setter.
private List<Transporter> transporters; //Getter only.
@Override
public ZoneTable getModel() {
return entity;
}
@Override
public void prepare() throws Exception {
transporters=sharableService.getTransporterList();
}
}
这样做不起作用。它在提交时未设置transporterId
的值,因为操作类正在实施ModelDriven<ZoneTable>
而不是ModelDriven<Transporter>
,就像第一种情况一样。
这是否可以使用模型驱动方法?
修改
ZoneTable.java
public class ZoneTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "zone_id", nullable = false)
private Long zoneId;
@Column(name = "zone_name", length = 45)
private String zoneName;
@JoinColumn(name = "transporter_id", referencedColumnName = "transporter_id")
@ManyToOne(fetch = FetchType.LAZY)
private Transporter transporterId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "zoneTable", fetch = FetchType.LAZY)
private Set<ZoneCharge> zoneChargeSet;
@OneToMany(mappedBy = "zoneId", fetch = FetchType.LAZY)
private Set<Country> countrySet;
//Getters and setters + constructors.
}
Zone.jsp
<s:form namespace="/admin_side" action="Zone" validate="true" id="dataForm" name="dataForm" cssClass="search_form general_form">
<s:label key="label.zone.name" for="zone"/>
<s:textfield id="zoneName" name="zoneName" cssClass="validate[required, maxSize[45], minSize[2]] text-input text"/>
<s:fielderror fieldName="zoneName"/>
<s:label key="label.transporter.name" for="transporterId"/>
<s:select id="transporterId" name="transporterId" list="transporters" value="transporterId" listKey="transporterId" listValue="transporterName" headerKey="" headerValue="Select" listTitle="transporterName"/>
<s:fielderror fieldName="transporterId"/>
<s:text name="label.submit"/>
<s:submit id="btnSubmit" name="btnSubmit" value="Submit" action="AddZone"/>
</s:form>
由于这篇文章已经有很多代码,我不会在这里发布动作类ZoneAction.java
。如果需要,可以使用here。
答案 0 :(得分:2)
您需要转换器才能将transporterId
转换为Transporter
对象。它是这样的:
package com.converter;
public class TransporterConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map map, String[] strings, Class type) {
String value = strings[0]; // The value of transporterId submitted from the jsp
if (value != null && value.length() > 0) {
try {
Long longVal = Long.valueOf(value);
//Integer intVal = Integer.valueOf(value);
if (type == Transporter.class) {
Transporter data = find_transporter_from_the_back_by_transporter_id_using_longVal;
return data;
}
} catch (Exception ex) {}
}
return null;
}
@Override
public String convertToString(Map map, Object o) {
if ((o instanceof Transporter)) {
Transporter data = (Transporter) o;
//return the id of the Transporter Object
}
return null;
}
}
接下来要做的是将此类映射到名为xwork-conversion.properties
的文件中。此文件必须位于类路径中,即classes
目录中。在xwork-conversion.properties
package_of_transporter_class.Transporter=com.converter.TransporterConverter
我还没有测试过,但我认为它应该可行。
如果您需要有关类型转换器工作方式的更多信息,请按照this url。