1、使用领域对象来接收用户输入
er对象中包含两个字段:username和password,在Action中有一个User对象user,则在jsp中:
Jsp代码
<input type="text" name="user.username">
<input type="text" name="user.password">
<input type="text" name="user.username">
<input type="text" name="user.password"> 来进行数据绑定。
当Struts2尝试设置user对象的username属性时,将依次调用以下的方法:
Java代码
action.getUser();
action.setUser(new User());
action.getUser().setUserName();
action.getUser();
action.setUser(new User());
action.getUser().setUserName();Struts2首先尝试获得User对象的引用,当它发现User对象为null时,它将使用User类的默认构造方法创建一个User实例,并调用action的setUser()方法设置这个实例。这样,Struts2就可以设置Usser对象的username字段了。
在Struts2中,自动实例化对象的特性对于任意层次的对象包含关系都是适用的。
例如, user.address.street这样的表达式会调用:
Java代码
getUser().getAdderss().setStreet(...);
getUser().getAdderss().setStreet(...); Struts2会先创建User对象,接着创建Address对象,然后设置Address对象的street属性。
2、使用ModelDriver action
Java代码
public class LoginAction implements Action, ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
}
public class LoginAction implements Action, ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
} 在jsp页面中:
Jsp代码
<input type="text" name="user.username">
<input type="text" name="user.password">
<input type="text" name="user.username">
<input type="text" name="user.password">
3、使用action的属性接收用户输入
Java代码
public class LoginAction implements Action, ModelDriven<User> {
private String username;
...
}
public class LoginAction implements Action, ModelDriven<User> {
private String username;
...
}
4、访问request、session和application对象
4.1 与 Servlet API 解耦的访问方式
Java代码
// 获得action的执行上下文
ActionContext context = ActionContext.getContext();
// 得到三个对象
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
// 使用三个对象
request.put("text1","test1");
session.put("text2","test2");
application.put("text2","test3");
// 获得action的执行上下文
ActionContext context = ActionContext.getContext();
// 得到三个对象
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
// 使用三个对象
request.put("text1","test1");
session.put("text2","test2");
application.put("text2","test3");使用JSP内置的表达式语言来访问request、session和application范围的数据:
Jsp代码
${seesionScope.user.username},${requestScope.text2}
${applicationScope.text3}
${seesionScope.user.username},${requestScope.text2}
${applicationScope.text3}通过接口注入来获取request、session和application对象
Java代码
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}
4.2 与 Servlet API 耦合的访问方式
通过ServletActionContext来获取HttpServletRequest和ServletContext对象
Java代码
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}
er对象中包含两个字段:username和password,在Action中有一个User对象user,则在jsp中:
Jsp代码
<input type="text" name="user.username">
<input type="text" name="user.password">
<input type="text" name="user.username">
<input type="text" name="user.password"> 来进行数据绑定。
当Struts2尝试设置user对象的username属性时,将依次调用以下的方法:
Java代码
action.getUser();
action.setUser(new User());
action.getUser().setUserName();
action.getUser();
action.setUser(new User());
action.getUser().setUserName();Struts2首先尝试获得User对象的引用,当它发现User对象为null时,它将使用User类的默认构造方法创建一个User实例,并调用action的setUser()方法设置这个实例。这样,Struts2就可以设置Usser对象的username字段了。
在Struts2中,自动实例化对象的特性对于任意层次的对象包含关系都是适用的。
例如, user.address.street这样的表达式会调用:
Java代码
getUser().getAdderss().setStreet(...);
getUser().getAdderss().setStreet(...); Struts2会先创建User对象,接着创建Address对象,然后设置Address对象的street属性。
2、使用ModelDriver action
Java代码
public class LoginAction implements Action, ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
}
public class LoginAction implements Action, ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
} 在jsp页面中:
Jsp代码
<input type="text" name="user.username">
<input type="text" name="user.password">
<input type="text" name="user.username">
<input type="text" name="user.password">
3、使用action的属性接收用户输入
Java代码
public class LoginAction implements Action, ModelDriven<User> {
private String username;
...
}
public class LoginAction implements Action, ModelDriven<User> {
private String username;
...
}
4、访问request、session和application对象
4.1 与 Servlet API 解耦的访问方式
Java代码
// 获得action的执行上下文
ActionContext context = ActionContext.getContext();
// 得到三个对象
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
// 使用三个对象
request.put("text1","test1");
session.put("text2","test2");
application.put("text2","test3");
// 获得action的执行上下文
ActionContext context = ActionContext.getContext();
// 得到三个对象
Map request = (Map)context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
// 使用三个对象
request.put("text1","test1");
session.put("text2","test2");
application.put("text2","test3");使用JSP内置的表达式语言来访问request、session和application范围的数据:
Jsp代码
${seesionScope.user.username},${requestScope.text2}
${applicationScope.text3}
${seesionScope.user.username},${requestScope.text2}
${applicationScope.text3}通过接口注入来获取request、session和application对象
Java代码
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}
4.2 与 Servlet API 耦合的访问方式
通过ServletActionContext来获取HttpServletRequest和ServletContext对象
Java代码
public class LoginAction implements Action, RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;
@Override
public void setRequest(Map request){
this.request = request;
}
@Override
public void setSession(Map session){
this.session = session;
}
@Override
public void setApplication(Map application){
this.application= application;
}
}