Struts2作为MVC框架,必须要负责解析HTTP请求参数,并将其封装到Model对象中。Struts2提供了非常强大的类型转换机制用于请求数据到model对象的封装。
一、方式一
1)Action本身作为model对象,通过成员setter封装。用Action动作类作为模型对象。
public class userAction extends ActionSupport {
private String username;
private String password;
public String regist()
{
if("pjm".equals(username) && "123".equals(password))
{
return SUCCESS;
}else
{
return "login";
}
}
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;
}
}
2)创建独立的Model对象,页面通过ognl封装(动作类和模型分开,推荐使用)
a.模型
public class Person implements Serializable{
// 属性和form表单中的字段名一致
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;
}
@Override
public String toString() {
return "Person [username=" + username + ", password=" + password + "]";
}
}
b.动作
public class PersonAction extends ActionSupport {
// 动作和模型分开
private Person person;
public Person getPerson() {
System.out.println("getter");
return person;
}
public void setPerson(Person person) {
System.out.println("setter");
this.person = person;
}
public String regist()
{
if("pjm".equals(person.getUsername()) && "123".equals(person.getPassword()))
{
return SUCCESS;
}else
{
return "login";
}
}
}
c.配置文件
<package name="re2" extends="struts-default" namespace="/person">
<action name="regist" class="com.pangu.action.PersonAction" method="regist">
<result name="success">/success.jsp</result>
<result name="login">/success.jsp</result>
</action>
</package>
d.表单页面
<form action="${pageContext.request.contextPath}/person/regist.action" method="post">
用户名: <input type="text" name="person.username"/><br/>
密码: <input type="password" name="person.password"/><br/>
<input type="submit" value="登陆"/>
</form>
3)使用modelDriven接口,对请求数据进行封装(模型驱动:ModelDriven)
注:该功能是由一个叫做modelDriven的拦截器完成的。
a. 模型和方式二一样
b.动作
// 实现ModelDriven接口
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private Customer customer = new Customer(); // 这里必须初始化
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String regist()
{
System.out.println(customer);
if("pjm".equals(customer.getUsername()) && "123".equals(customer.getPassword()))
{
return SUCCESS;
}else
{
return "login";
}
}
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
}
c、配置文件
<package name="re3" extends="struts-default" namespace="/customer">
<action name="regist" class="com.pangu.action.CustomerAction" method="regist">
<result name="success">/success.jsp</result>
<result name="login">/success.jsp</result>
</action>
</package>
d 、表单页面
<form action="${pageContext.request.contextPath}/customer/regist.action" method="post">
用户名: <input type="text" name="username"/><br/>
密码: <input type="password" name="password"/><br/>
<input type="submit" value="登陆"/>
</form>
网友评论