关于Struts2,前台表单传值到后台方式

来源:互联网 发布:mac web开发工具 编辑:程序博客网 时间:2024/06/10 15:59


正在学习Struts2框架,今天做了一个demo,登录功能,前端from表单提交账号密码信息。

<body>
   <form action="login.action" method="post" name="from1">
账号:<input type="text" name="userId"/>
密码:<input type="text" name="userPw"/>
<input type="submit" value="登录"/>  
   </form>
  </body>

web.xml配置文件里

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 
</filter>
 
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
 
</filter-mapping>

在Action类里如下:

private String userId;
private String userPw;
@Override
public String execute() throws Exception {
System.out.println("账号:"+userId);
System.out.println("密码:"+userPw);
if("admin".equals(userId)&&"123".equals(userPw)){
return SUCCESS;
}
return "fail";
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserPw() {
return userPw;
}
public void setUserPw(String userPw) {
this.userPw = userPw;
}

在控制台打印输出账号密码,刚开始的时候一直显示为null。

后来才知道在web.xml里<filter-mapping>里设置的后缀为action。

把前端from表单属性里的action=“login” 改为action=“login.action”,就行了。



0 0