Create and parameter passing and receiving class Struts2 Action

A, Struts in Action was created way

1, directly create a simple Action class

Add Struts.xml, configured to forward forwarding method returns a page.

 

 

2, to achieve a Class Action

 

Strust.xml Url forward mapping corresponding to the configuration.

 

3, inherit a class ActionSupport

 

Struts.xml contents of the configuration file is as follows:

Two, Struts in Action acquired in Servlet API, and the stored value of servlet objects operate through the domain

1, to obtain the request field, session domain, the context object file application domain through ActionContext

 

struts.xml profile configuration elements:

 

 

Forwarding page content:

 

Action Get Servlet the API, and the stored value of servlet objects operate through the domain

2, to obtain the request field, session domain, the context object file application domain through servletActionContext

 

strust.xml profile

 

Forwarding page

3, Action class to get request field, session domain, application domain by implementing ServletRequestAware, ServletContextAware context object

Struts.xml configuration file as follows:

jsp forwarding configuration page

 

 

Three, Struts in Action in three ways to get the passed parameters page

1, to obtain the corresponding parameter values ​​of the class attribute Action

package com.java.test.param.Action;
import java.util.Date;
public class ParamAction {
private String name;
private Integer age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String test() {
System.out.println("name:"+name);
System.out.println("age:"+age);
System.out.println("birthday:"+birthday);
return "test";
}
}

 struts.xml配置页面

form.jsp

welcome.jsp

2,通过Action类的对象属性来获取相应的参数值

#user.java
package com.java.test.param.Action;

import java.util.Date;

public class User {

private String name;
private Integer age;
private Date birthday;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}



}

#Param1Action .java
package com.java.test.param.Action;

public class Param1Action {
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String test() {
return "test";
}



}

strust.xml配置文件

 

form1.jsp

welcome1.jsp

 

3,通过实现ModelDriven类的来获取相应的参数值

 

#Param3.java
package com.java.test.param.Action;

import com.opensymphony.xwork2.ModelDriven;

public class Param3 implements ModelDriven{
private User user = new User();

@Override
public User getModel() {
// TODO Auto-generated method stub return user;
}

public String test() {
return "test";
}


}

 struts.xml

form2.jsp

welcome2.jsp

 

四、Struts中如何传递list,map参数

 

#Param4Action .java
package com.java.test.param.Action;

import java.util.List;
import java.util.Map;

public class Param4Action {
private List list;
private Map<String,String> map;

public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}

public String test() {
return "test";
}

}

form3.jsp

welcome3.jsp

 

 

总结:1,创建Action有三种方式,每一种都有对应的好处,建议使用第二种或者第三种,这种方式能够继承或者实现父类,从而可以复用父类的一些东西。

            2,获取servlet Api,建议使用第二种方式,和servlet的方式类似。但struts中不建议这样来获取参数或设置对象域。

            3, Action类中接收参数的三种方式,第一种的话,很零散,操作数据库还得进行封装类,第二种方式,可以直接封装好类,这种方式可以,第三种,这种方式也可以,就是               每次只能实现一个bean类,从而只能获取一个bean。多个bean的话不太好解决。

            最后一个就是传递list参数和map参数,list就是页面上得有多个对应的list,map必须指定对应的key,不然不知道是哪一个key需要存值。

   

Guess you like

Origin www.cnblogs.com/Hackerman/p/11225112.html