1.Struts2的Action接收参数的三种形式。
a. 使用Action的属性接收(直接在action中利用get方法来接收参数):
login.jsp
< form action= "LoginAction" method = "post">
< input type= "text" name = "username">< br />
< input type= "password" name = "userpwd">< br />
< input type= "submit" value = "提交">
LoginAction.java
public class LoginAction extends ActionSupport {
public String username ;
public String userpwd ; public String getUsername() {
return username ;
} public void setUsername(String username) {
this .username = username ;
} public String getUserpwd() {
return userpwd ;
} public void setUserpwd(String userpwd) {
this .userpwd = userpwd ;
}
public String execute(){
String result= "input" ;
if (getUsername().equals("lichenyu" )&&getUserpwd().equals( "123456")){
// result="input";
} else {
result= "error" ;
}
return result ; }
}
b.使用 DomainModel接收参数:
login.jsp
< form action= "LoginAction" method = "post">
< input type= "text" name = "user.username">< br />
< input type= "password" name = "user.userpwd">< br />
< input type= "submit" value = "提交">
</ form>
新建User.java
public class User {
public String username ;
public String userpwd ; public String getUsername() {
return username ;
}
public void setUsername(String username) {
this .username = username ;
}
public String getUserpwd() {
return userpwd ;
}
public void setUserpwd(String userpwd) {
this .userpwd = userpwd ;
} }
LoginAction.java(此处必须添加user的set和get方法)
public class LoginAction extends ActionSupport {
private User user ; public User getUser() {
return user ;
} public void setUser(User user) {
this .user = user ;
} public String execute(){
String result= "input" ;
System. out .println(user .username );
return result ;
}
}
c.使用 ModelDriven接收参数:
login.jsp
public class LoginAction extends ActionSupport {
private User user ; public User getUser() {
return user ;
} public void setUser(User user) {
this .user = user ;
} public String execute(){
String result= "input" ;
System. out .println(user .username );
return result ;
}
}
User.java同方法b中
LoginAction.java
public class LoginAction extends ActionSupport implements ModelDriven<User> {
User user= new User();
public String execute(){
String result= "input" ;
System. out .println(user .username );
System. out .println(user .getUsername());
return result ;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user ;
}
}
d.使用request来接收参数:
使Action支持request后可用此方法进行传值。
HttpServletRequest request=ServletActionContext.getRequest();
String username=request.getParameter("username");
**********************************************************************************************************************
如若转载请注明出处By奋斗的小蘑菇