struts2_day03_Servlet的API的访问&&数据的封装

1、使用servletAPI的三种方式

1.1、什么是servletAPI

访问的就是Servlet规范中的常用对象:ServletContext、HttpSession、HttpServletRequest、HttpServletResponse

1.2、第一种方式(原生)

使用ServletActionContext对象的中方法访问Servlet的API

文档:struts-2.3.24/docs/struts2-core-apidocs/index.html

1.3、第二种方式(完全解耦合)

使用ActionContext对象中的方法访问Servlet的API

文档:struts-2.3.24/docs/xwork-apidocs/index.html

常用的方法说明
public Object get(String key)Returns a value that is stored in the current ActionContext by doing a lookup using the value's key
public void put(String key, Object value)Stores a value in the current ActionContext
public static ActionContext getContext() Returns the ActionContext specific to the current thread
public Map<String,Object> getParameters() Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of parameters otherwise
public Map<String,Object> getApplication() Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise
public Map<String,Object> getSession() Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise
public ValueStack getValueStack() Gets the OGNL value stack
public void setApplication(Map<String,Object> application)Sets the action's application context
public void setSession(Map<String,Object> session)Sets a map of action session values
public void setValueStack(ValueStack stack)Sets the OGNL value stack

1.4、第三种方式(实现接口)

在Action中实现如下的一些接口:

org.apache.struts2.util.ServletContextAware:实现该接口的Action可以访问ServletContext对象

org.apache.struts2.interceptor.ServletRequestAware:实现该接口的Action可以访问ServletRequest对象

org.apache.struts2.interceptor.ServletResponseAware:实现该接口的Action可以访问ServletResponse对象

org.apache.struts2.interceptor.SessionAware:实现该接口的Action可以访问HttpSession对象

2、数据的封装

2.1属性驱动

2.1.2 Action动作类中添加属性的set方法

  • 页面

<%@ 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>
	<h1>数据封装方式:属性驱动-提供属性的set方法的方式</h1>
	<form action="${pageContext.request.contextPath }/demo1.action" method="post">
		用户名:<input type="text" name="username"/><br/>
		密码:<input type="password" name="password"/><br/>
		性别:<input type="radio" name="sex" value="男"/>男
			<input type="radio" name="sex" value="女"/>女<br/>
		爱好:<input type="checkbox" name="hobby" value="唱歌"/>唱歌
			<input type="checkbox" name="hobby" value="喝酒"/>喝酒
			<input type="checkbox" name="hobby" value="编码"/>编码<br/>
		生日:<input type="text" name="birthday"/><br/>
		<input type="submit" value="提交"/>
	</form>
	
</body>
</html>
  • 配置文件

<?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" />

	<package name="default" namespace="/" extends="struts-default">

		<!-- 数据封装方式:属性驱动-set方法方式 -->
		<action name="demo1" class="com.itheima.web.Demo01Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>
  • 动作类
package com.itheima.web;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: Demo01Action
 * @Description:数据封装方式:属性驱动-属性的set方法的方式
 * @author jsz
 * @date 2018年8月24日
 */
public class Demo01Action extends ActionSupport {

	private String username;
	private String password;
	private String sex;
	private String hobby;
	private Date birthday;

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String register() {
		System.out.println(username);
		System.out.println(password);
		System.out.println(sex);
		System.out.println(hobby);
		System.out.println(birthday);
		return null;
	}
}

2.1.2 表达式的方式

编写Javabean

action动作类中提供set/get方法

  • 页面
<%@ 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>

	<h1>数据封装方式:属性驱动-表达式的方式</h1>
	<form action="${pageContext.request.contextPath }/demo2.action" method="post">
		用户名:<input type="text" name="user.username"/><br/>
		密码:<input type="password" name="user.password"/><br/>
		性别:<input type="radio" name="user.sex" value="男"/>男
			<input type="radio" name="user.sex" value="女"/>女<br/>
		爱好:<input type="checkbox" name="user.hobby" value="唱歌"/>唱歌
			<input type="checkbox" name="user.hobby" value="喝酒"/>喝酒
			<input type="checkbox" name="user.hobby" value="编码"/>编码<br/>
		生日:<input type="text" name="user.birthday"/><br/>
		<input type="submit" value="提交"/>
	</form>

</body>
</html>
  • Javabean
package com.itheima.domain;

import java.util.Date;

public class User {

	private String username;
	private String password;
	private String sex;
	private String hobby;
	private Date birthday;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(String username, String password, String sex, String hobby, Date birthday) {
		super();
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.hobby = hobby;
		this.birthday = birthday;
	}

	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;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", sex=" + sex + ", hobby=" + hobby
				+ ", birthday=" + birthday + "]";
	}
}
  • 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" />

	<package name="default" namespace="/" extends="struts-default">

		<!-- 数据封装方式:属性驱动-表达式方式 -->
		<action name="demo2" class="com.itheima.web.Demo02Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>
  • 动作类
package com.itheima.web;

import com.itheima.domain.User;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: Demo02Action 
 * @Description:数据封装方式:属性驱动-表达式的方式
 * @author jsz
 * @date 2018年8月24日
 */
public class Demo02Action extends ActionSupport {

	private User user;

	public User getUser() {
		return user;
	}

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

	public String register() {
		System.out.println(user);
		return null;
	}
}

2.2模型驱动

编写Javabean

动作类中创建Javabean实例

实现ModelDriven<T>接口,重写方法

页面

<%@ 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>
	<h1>数据封装方式:模型驱动方式</h1>
	<form action="${pageContext.request.contextPath }/demo3.action" method="post">
		用户名:<input type="text" name="username"/><br/>
		密码:<input type="password" name="password"/><br/>
		性别:<input type="radio" name="sex" value="男"/>男
			<input type="radio" name="sex" value="女"/>女<br/>
		爱好:<input type="checkbox" name="hobby" value="唱歌"/>唱歌
			<input type="checkbox" name="hobby" value="喝酒"/>喝酒
			<input type="checkbox" name="hobby" value="编码"/>编码<br/>
		生日:<input type="text" name="birthday"/><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

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" />

	<package name="default" namespace="/" extends="struts-default">

		<!-- 数据封装方式:模型驱动方式 -->
		<action name="demo3" class="com.itheima.web.Demo03Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>

action动作类

package com.itheima.web;

import com.itheima.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @ClassName: Demo03Action 
 * @Description:数据封装方式:模型驱动
 * @author jsz
 * @date 2018年8月24日
 */
public class Demo03Action extends ActionSupport implements ModelDriven<User>{

	private User user = new User();
	@Override
	public User getModel() {
		return user;
	}
	
	public String register() {
		System.out.println(user);
		return null;
	}
}

数据转换异常

日期格式转换异常

12:19:24.077 [http-bio-8080-exec-5] ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'birthday' on 'class com.itheima.web.Demo03Action: Error setting expression 'birthday' with value ['20180810', ]

解决

<?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" />

	<package name="default" namespace="/" extends="struts-default">

		<global-results>
			<result name="input">/register.jsp</result>
		</global-results>
		
		<!-- 数据封装方式:模型驱动方式 -->
		<action name="demo3" class="com.itheima.web.Demo03Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>

将数据封装到集合中

list集合

页面

<%@ 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>
	<h1>封装数据到集合中:list</h1>
	<form action="${pageContext.request.contextPath }/demo4.action" method="post">
		用户名1:<input type="text" name="list[0].username"/>  
		密码1:<input type="password" name="list[0].password"/><br/>
		用户名2:<input type="text" name="list[1].username"/>
		密码2:<input type="password" name="list[1].password"/><br/>
		
		<input type="submit" value="提交"/>
	</form>

</body>
</html>
<%@ 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>
	<h1>成功</h1>
</body>
</html>

 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" />

	<package name="default" namespace="/" extends="struts-default">

		<global-results>
			<result name="input">/register.jsp</result>
		</global-results>
		
		<!-- 将数据封装到list集合中 -->
		<action name="demo4" class="com.itheima.web.Demo04Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>

action动作类

package com.itheima.web;

import java.util.List;

import com.itheima.domain.User;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: Demo04Action
 * @Description:将数据封装到list集合中
 * @author jsz
 * @date 2018年8月24日
 */
public class Demo04Action extends ActionSupport {

	private List<User> list;

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}

	public String register() {
		System.out.println(list);
		return SUCCESS;
	}
}

 map集合

页面

<%@ 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>
	<h1>封装数据到集合中:map</h1>
	<form action="${pageContext.request.contextPath }/demo5.action" method="post">
		用户名1:<input type="text" name="map['one'].username"/>  
		密码1:<input type="password" name="map['one'].password"/><br/>
		用户名2:<input type="text" name="map['two'].username"/>
		密码2:<input type="password" name="map['two'].password"/><br/>
		
		<input type="submit" value="提交"/>
	</form>
</body>
</html>
<%@ 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>
	<h1>成功</h1>
</body>
</html>

 

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" />

	<package name="default" namespace="/" extends="struts-default">

		<global-results>
			<result name="input">/register.jsp</result>
		</global-results>
		
		<!-- 将数据封装到map集合中 -->
		<action name="demo5" class="com.itheima.web.Demo05Action" method="register">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>

action动作类

package com.itheima.web;

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

import com.itheima.domain.User;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: Demo05Action
 * @Description:将数据封装到map集合中
 * @author jsz
 * @date 2018年8月24日
 */
public class Demo05Action extends ActionSupport {

	private Map<String, User> map;

	public Map<String, User> getMap() {
		return map;
	}

	public void setMap(Map<String, User> map) {
		this.map = map;
	}

	public String register() {
		System.out.println(map);
		return SUCCESS;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值