作为MVC框架,必须要负责解析Http请求参数,并将其封装到Model对象中
Struts2提供了非常强大的类型转换机制用于请求数据封装到model对象
静态参数封装:
struts.xml
<!--静态参数的封装-->
<package name="staticparam" extends="struts-default">
<action name="staticParam" class="com.wangyjie.struts.staticparam.StaticParamAction" method="login">
<param name="username">社会王</param>
<param name="password">111</param>
</action>
</package>
Action:
/**
* Created by dllo on 17/7/5.
* 静态参数封装:
* 拦截器:staticParam
*/
public class StaticParamAction {
private String username;
private String password;
public String login(){
System.out.println("u: " + username + "pass: " + password);
return null;
}
//静态参数要有set和get方法
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;
}
}
底层依靠 staticParams 拦截器, 将静态参数放入值栈中,而action就在值栈中的栈顶, 自然就会找到该action中的对应属性,然后进行赋值.
动态参数封装
属性驱动
普通属性驱动,提供set get方法
Action:
/**
* Created by dllo on 17/7/5.
* 动态参数封装: 属性驱动
* 普通属性
*/
public class UserAction extends ActionSupport {
private String username;
private String password;
public String login(){
System.out.println("user: " + username + "pass: " + password);
return null;
}
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;
}
}
jsp:
<form action="${pageContext.request.contextPath}/login1.action" method="post">
用户名:<input type="text" name="username"><br/>
密 码:<input type="text" name="password"><br/>
<input type="submit" value="登录">
</form>
跟静态参数封装一样,只不过这里获取的是表单中的参数,也就是请求发送过来的数 据。依靠的拦截器为params. 其中该拦截器做的事有两件,一是对提交的参数进行数据 校验,判断是否合法,判断是否合法的标准就是拦截器中的excludeParams参数的正则 表达式的值。二是将其封装到值栈中的栈顶元素中去,而当前action就在栈顶,所以能 够将参数放入action中。
动态参数封装:属性驱动
Action:
/**
* Created by dllo on 17/7/5.
* 动态参数封装: 属性驱动
* 复杂属性: OGNL
*/
public class PersonAction extends ActionSupport{
private Person person;
public String login(){
System.out.println("person: " + person);
return null;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
javaBean Person类
public class Person {
private String username;
private String password;
public Person() {
}
public Person(String password, String username) {
this.password = password;
this.username = username;
}
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;
}
@Override
public String toString() {
return "Person{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
jsp
<form action="${pageContext.request.contextPath}/login2.action" method="post">
<%--这个就是OGNL表达式的使用--%>
用户名:<input type="text" name="person.username"><br/>
密 码:<input type="text" name="person.password"><br/>
<input type="submit" value="登录">
</form>
struts.xml
<!--动态参数封装 属性驱动-->
<package name="param" extends="struts-default">
<action name="login1" class="com.wangyjie.struts.param.UserAction" method="login">
</action>
<action name="login2" class="com.wangyjie.struts.param.PersonAction" method="login"></action>
</package>
在jsp页面中的 person.username 和person.password 其实就是ognl表达式,代表着往根 (root,值栈valueStack)中存放值,而值栈中的栈顶元素也就是为当前action,我们在 action中设置person的get、set属性,即可以让存进来的值匹配到,进而将对应属性赋 值成功。
动态参数封装:模型驱动
Action:
/**
* Created by dllo on 17/7/5.
* 动态参数封装:模型驱动
* 拦截器: modelDriven
* getModel返回时必须是实例化的对象,位置不限
*/
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private Customer customer = new Customer();
public String login(){
System.out.println("customer: " + customer.getUsername() + customer.getPassword());
return SUCCESS;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public Customer getModel() {
return customer;
}
}
jsp:
<form action="${pageContext.request.contextPath}/login3.action" method="post">
<%--这个就是OGNL表达式的使用--%>
用户名:<input type="text" name="username"><br/>
密 码:<input type="text" name="password"><br/>
<input type="submit" value="登录">
</form>