S2SH:struts2获取前台数据的三种方式
程序员文章站
2022-04-17 20:47:36
...
struts2 获取前台数据的三种方式:
1.使用Action的属性
2.使用DomainModel(域模型,用JavaBean 做action的属性)
3.使用ModelDriven(体现MVC思想)
一:使用Action的属性接受参数
方法是,在对应的login.action中设置两个String变量,变量名为username,password.(变量名可以随便取), 然后使用get,set方法。Action会调用setXXX方法来获取值。
注:当属性为public 时,可以不使用get,set方法;当为private时,必须有get,set方法.
jsp:
<s:form name="form1" action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action中:
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
if (!username.equals("admin")) {
super.addFieldError("username", "用户名错误!");
return ERROR;
}
if (!password.equals("001")) {
super.addFieldError("password", "密码错误!");
return ERROR;
}
}
}
public void validate(){
System.out.println("----------username="+username+"-----password="+password+"----------------");
if(username==null || username.length()==0){
super.addActionError("用户名不能为空");
}
if(password==null || password.length()==0){
super.addActionError("密码不能为空");
}
}
二:使用DomainModel
struts2 使用DomainModel 来获取前台参数 在Action中,设置一个对象来接收前台参数,传参时参数变量为类名+变量名一一对应;
jsp:
<s:form name="form1" action="login">
<s:textfield name="user.username" label="username"></s:textfield>
<s:password name="user.password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action:
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() {
if(user==null){
System.out.println("--------no user-------------");
return ERROR;
}else{
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
return SUCCESS;
}
}
public void validate(){
//TODO
}
三:使用ModelDriven
方法是实现ModelDriven借口,传参直接使用对象的属性为变量,必须new一个新的对象
jsp:
<s:form name="form1" action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action:
private User user=new User();
public User getModel() {
return user;
}
public String execute() {
if(user==null){
System.out.println("--------no user-------------");
return ERROR;
}else{
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
return SUCCESS;
}
}
public void validate(){
//TODO
}
1.使用Action的属性
2.使用DomainModel(域模型,用JavaBean 做action的属性)
3.使用ModelDriven(体现MVC思想)
一:使用Action的属性接受参数
方法是,在对应的login.action中设置两个String变量,变量名为username,password.(变量名可以随便取), 然后使用get,set方法。Action会调用setXXX方法来获取值。
注:当属性为public 时,可以不使用get,set方法;当为private时,必须有get,set方法.
jsp:
<s:form name="form1" action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action中:
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
if (!username.equals("admin")) {
super.addFieldError("username", "用户名错误!");
return ERROR;
}
if (!password.equals("001")) {
super.addFieldError("password", "密码错误!");
return ERROR;
}
}
}
public void validate(){
System.out.println("----------username="+username+"-----password="+password+"----------------");
if(username==null || username.length()==0){
super.addActionError("用户名不能为空");
}
if(password==null || password.length()==0){
super.addActionError("密码不能为空");
}
}
二:使用DomainModel
struts2 使用DomainModel 来获取前台参数 在Action中,设置一个对象来接收前台参数,传参时参数变量为类名+变量名一一对应;
jsp:
<s:form name="form1" action="login">
<s:textfield name="user.username" label="username"></s:textfield>
<s:password name="user.password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action:
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() {
if(user==null){
System.out.println("--------no user-------------");
return ERROR;
}else{
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
return SUCCESS;
}
}
public void validate(){
//TODO
}
三:使用ModelDriven
方法是实现ModelDriven借口,传参直接使用对象的属性为变量,必须new一个新的对象
jsp:
<s:form name="form1" action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror />
Action:
private User user=new User();
public User getModel() {
return user;
}
public String execute() {
if(user==null){
System.out.println("--------no user-------------");
return ERROR;
}else{
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
return SUCCESS;
}
}
public void validate(){
//TODO
}