本系列博客汇总在这里:Struts2 汇总
参数的封装
- Jsp 页面
- model
- 动作类
- Struts.xml 配置
- 结果
- Jsp 页面
- model
- 动作类
- Struts.xml 配置
- 结果
一、静态参数的封装
在运行器不发生变化的数据,或者一些配置相关的数据可以做静态封装。
1、Action 动作类
package com.wyx.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 参数的封装
*/
public class PersonAction extends ActionSupport
{
/**
* 用于接收静态的参数,必须提供set和get方法
*/
private String name = "魏宇轩";
private String desc;
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String hello()
{
System.out.println(name);
System.out.println(desc);
return super.SUCCESS;
}
}
2、Struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
开发模式
-->
<constant name="struts.devMode" value="true"></constant>
<package name="person" extends="struts-default" namespace="/person">
<action name="wyx1" class="com.wyx.action.PersonAction" method="hello">
<!--
param的name在Action中必须要有相应的属性
-->
<param name="name">魏宇轩同学</param>
<param name="desc">爱生活,爱夫人!</param>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
3、结果
二、动态参数的封装
系统运行期间用户提交表单,ajax,url 访问。
1、动作类充当模型对象
动作类和模型和为一体,我们可以在动作类中定义要接收的属性的值,对每个属性必须要提供set 和 get 方法,动作类 model 中的属性的名字和表单中的 name 一定要一致,否则无法注入。
Jsp 页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/person/wyx2" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="1" checked="checked">男
<input type="radio" name="gender" value="2">女
</td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address"></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</body>
</html>
动作类 model
package com.wyx.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 参数的封装
*/
public class PersonAction1 extends ActionSupport
{
/**
* 在Action的内部定义model的属性
*/
private int id;
private String name;
private int gender;
private String address;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getGender()
{
return gender;
}
public void setGender(int gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String hello()
{
System.out.println(id);
System.out.println(name);
System.out.println(gender);
System.out.println(address);
return super.SUCCESS;
}
}
Struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
开发模式
-->
<constant name="struts.devMode" value="true"></constant>
<package name="person" extends="struts-default" namespace="/person">
<action name="wyx2" class="com.wyx.action.PersonAction1" method="hello">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
结果
2、动作类和模型对象分离封装参数(推荐使用)
- 动作类充当模型对象 Action 既是是 C 又是 M,可读性差,重用性差,很难维护,不推荐使用。
- 动作类和模型对象分离封装参数这种方式接受参数对提交的表单中的 name 有要求,name 需要使用 Action 中的 model 属性的名字加点再加上要接收的属性名:如 person.name。
Jsp 页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/person/wyx3" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="person.id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="person.name"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="person.gender" value="1" checked="checked">男
<input type="radio" name="person.gender" value="2">女
</td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="person.address"></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</body>
</html>
model
package com.wyx.model;
public class Person
{
private int id;
private String name;
private int gender;
private String address;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getGender()
{
return gender;
}
public void setGender(int gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", address=" + address + "]";
}
}
动作类
动作类里面需要定义 model 的属性并且提供 set 和 get 方法!
package com.wyx.action;
import com.opensymphony.xwork2.ActionSupport;
import com.wyx.model.Person;
/**
* action动作类充当模型对象
*/
public class PersonAction2 extends ActionSupport
{
/**
* 定义model类的对象用于接收参数 必须提供set和get
*/
private Person person;
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public String hello()
{
System.out.println(person);
return super.SUCCESS;
}
}
Struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
开发模式
-->
<constant name="struts.devMode" value="true"></constant>
<package name="person" extends="struts-default" namespace="/person">
<action name="wyx3" class="com.wyx.action.PersonAction1" method="hello">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
结果
3、模型驱动方式封装参数(推荐使用)
第二种方式对页面上的文本域的 name 有要求必须要用 model 的属性的名称.属性名
,这样的话页面和 Action 动作类有侵入性,模型驱动方式解决了这个问题,我们需要实现一个ModelDriven 接口指定要接收的 model 的类型提供 getModel 的方法,Action 类中的 model 对象必须要手动的创建,否则无法注入属性值。
Jsp 页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/person/wyx4" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="1" checked="checked">男
<input type="radio" name="gender" value="2">女
</td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address"></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</body>
</html>
model
package com.wyx.model;
public class Person
{
private int id;
private String name;
private int gender;
private String address;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getGender()
{
return gender;
}
public void setGender(int gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", address=" + address + "]";
}
}
动作类
package com.wyx.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wyx.model.Person;
/**
* action动作类充当模型对象
*/
public class PersonAction3 extends ActionSupport implements ModelDriven<Person>
{
/**
* 定义model类的对象用于接收参数 必须提供set和get,model的对象必须手动的创建,否则框架无法注入属性
*/
private Person person = new Person();
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public String hello()
{
System.out.println(person);
return super.SUCCESS;
}
@Override
public Person getModel()
{
return person;
}
}
Struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
开发模式
-->
<constant name="struts.devMode" value="true"></constant>
<package name="person" extends="struts-default" namespace="/person">
<action name="wyx4" class="com.wyx.action.PersonAction1" method="hello">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
结果
如有错误,欢迎指正!