struts2中的actions接收不到前台form表单的值
使用struts标签提交<s:formaction="student_action"method="post"><s:textfieldlabel="用户名"name="...
使用struts标签提交
<s:form action="student_action" method="post">
<s:textfield label="用户名" name="name"></s:textfield>
<s:password label="密码" name="password"></s:password>
<s:password label="重复密码" name="repassword"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
action
@Component("student_action")
@Scope("prototype")
public class StudentAction extends ActionSupport {
private StudentDAO sdao;
private String name;
private String password;
private String repassword;
public StudentDAO getSdao() {
return sdao;
}
@Resource
public void setSdao(StudentDAO sdao) {
this.sdao = sdao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
@Override
public String execute() throws Exception {
System.out.println(name);
System.out.println(password);
System.out.println(repassword);
if(name!=null&&password.equals(repassword)){
System.out.println(2);
sdao.addStudent(name, password);
return Action.SUCCESS;
}else{
return Action.ERROR;
}
}
}
前台打印三个值全是null,怎么接收不到呢? 展开
<s:form action="student_action" method="post">
<s:textfield label="用户名" name="name"></s:textfield>
<s:password label="密码" name="password"></s:password>
<s:password label="重复密码" name="repassword"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
action
@Component("student_action")
@Scope("prototype")
public class StudentAction extends ActionSupport {
private StudentDAO sdao;
private String name;
private String password;
private String repassword;
public StudentDAO getSdao() {
return sdao;
}
@Resource
public void setSdao(StudentDAO sdao) {
this.sdao = sdao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
@Override
public String execute() throws Exception {
System.out.println(name);
System.out.println(password);
System.out.println(repassword);
if(name!=null&&password.equals(repassword)){
System.out.println(2);
sdao.addStudent(name, password);
return Action.SUCCESS;
}else{
return Action.ERROR;
}
}
}
前台打印三个值全是null,怎么接收不到呢? 展开
1个回答
展开全部
<s:form action="student_action" method="post">这句话里的
action属性的值,要根据Struts2的配置文件struts.xml里的设置来定:
比如struts.xml里是这样的:
...
<action name="student_action123" value="student_action"/>
...
那么页面里就应该写(假设是struts缺省包和默认命名空间):
<s:form action="student_action123.action" method="post">
也就是说,提交表单时的目标是配置文件中action的name属性决定的。
加不加.action后缀,可以看struts.xml里的配置常数
<constant name="struts.action.extension" value=",action"/>
来定,如这个设置就是加不加后缀都可以(好像是struts2的高版本都默认可以不加后缀)。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询