struts2采用域模型方式接收参数,出现user对象为空?! 如果将属性放入action中,并生成set get方法,可以
struts.xml:<struts><constantname="struts.i18n.encoding"value="gbk"/><constantname="st...
struts.xml:
<struts>
<constant name="struts.i18n.encoding" value="gbk" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.sd.action.TestAction">
<result>
/rec.jsp
</result>
</action>
</package>
</struts>
-----------------------------------------
index.jsp:
<form action="user/user!rec" method="post">
姓名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"/>
</form>
--------------------------
Action:
public class TestAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String rec() {
if(user != null) {
System.out.println("username=" + user.getUsername());
System.out.println("password=" + user.getPassword());
} else {
System.out.println(user);
}
return SUCCESS;
}
-----------------------------------
User类我就不写了,两个属性+get set方法! 展开
<struts>
<constant name="struts.i18n.encoding" value="gbk" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.sd.action.TestAction">
<result>
/rec.jsp
</result>
</action>
</package>
</struts>
-----------------------------------------
index.jsp:
<form action="user/user!rec" method="post">
姓名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"/>
</form>
--------------------------
Action:
public class TestAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String rec() {
if(user != null) {
System.out.println("username=" + user.getUsername());
System.out.println("password=" + user.getPassword());
} else {
System.out.println(user);
}
return SUCCESS;
}
-----------------------------------
User类我就不写了,两个属性+get set方法! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询