我们假设我们有这样的JSP:
<s:form action="MyActionClass" namespace="/sec">
<s:hidden name="userId" value="%{userId}"
<s:textfield name="name" value="%{name}" label="Name" />
<s:textfield name="location" value="%{location}" label="Location" />
</s:form>
这样的动作类:
public class MyActionClass extends ActionSupport {
private String userId;
private String name;
private String location;
//getters and setters
}
这很好用,我在提交表单上的jsp的action字段中获取值。我想要做的是创建另一个bean类,将字段放在该bean类中,将bean类声明为action类的属性并填充其字段。这样的事情。
public class MyActionClass extends ActionSupport {
private UserDTO user;
//getters and setters
}
UserDTO类看起来像这样。
public class UserDTO{
private String userId;
private String name;
private String location;
public UserDTO(){
}
//getters and setters
}
我试过这样做:
<s:form action="MyActionClass" namespace="/sec">
<s:bean name="com.actions.DTOs.UserDTO" var="user">
<s:hidden name="user.userId" value="%{user.userId}"
<s:textfield name="user.name" value="%{user.name}" label="Name" />
<s:textfield name="user.location" value="%{user.location}" label="Location" />
</s:bean>
</s:form>
答案 0 :(得分:2)
试试这个:
你的表格jsp:
<s:form action="MyActionClass" namespace="/sec">
<s:textfield name="user.userId" value="" label="UserId" />
<s:textfield name="user.name" value="" label="Name" />
<s:textfield name="user.location" value="" label="Location" />
<s:submit/>
</s:form>
MyActionClass.java:
public class MyActionClass extends ActionSupport implements ModelDriven<UserDTO>{
private UserDTO user;
public UserDTO getUser() {
return user;
}
public void setUser(UserDTO user) {
this.user = user;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml配置:
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="mypackage" namespace="/sec" extends="struts-default">
<action name="actionWhichResultsToYourFormJSPHere">
<result>yourFormJspHere.jsp</result>
</action>
<action name="MyActionClass" class="sec.MyActionClass">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
你的result.jsp:
<body>
id=${user.userId}<br>
name=${user.name}<br>
location=${user.location}<br>
</body>