【struts2】赵雅智_Struts2中结果集类型

整合Hibernate技术

分析的servlet

   客户端--->web容器-->web.xml -->servlet来处理 ----->model-->数据库

     <--------------------------------|

request.setAttribute(“username”,username);

//转发

request.getDis(“manager/index.jps”).forward(request,response);

       ---jsp页面中  ${username}

//重定向

response.sendRiedirect();

分析 struts2

客户端----->web容器--->web.xml-->struts2过滤器--->struts.xml--->Action--->model--->数据库

 <------------------------------------------------------------------------------------|

Action  要想把数据 传递给jsp

     private String username;

      public String getUsername(){return username;}

 通过什么方式传递 

   <result type="dispatcher" name="success">/manager/index.jsp</result>

Jsp =---〉 ${username}

Struts2中结果集类型

1、 每个action方法都返回一个String类型的值,struts一次请求返回什么值是由这个值确定的。

2、 在配置文件中,每一个action元素的配置都必须有result元素,每一个result对应一个action的返回值。

3、 Result有两个属性:

name:结果的名字,和action中的返回值一样,默认值为success;

type:响应结果类型,默认值为dispatcher.

下面找到struts-default.xml文件中,如下面所示:

             

说明:

1、 从上述可以看出总共10种类型

2、 默认类型为ServletDispatcherResult即转发。

3、 结果类型可以是这10种结果类型的任意一种。

Dispatcher类型
(1)、 说明

Dispatcher类型是最常用的结果类型,也是struts框架默认的结果类型。

(2)、 例子

页面参照:/manager/index.jsp

Action参照:AdminAction

配置文件: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>




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


		<action name="adminLogin" class="www.csdn.project.action.AdminAction"
			method="login">
			<!--
				第一种写法 <result name="success">/manager/index.jsp</result>
			-->
			<!-- 第二种写法 -->
			<result name="success">
				<param name="location">/manager/index.jsp</param>
			</result>
			<result name="login">/index.jsp</result>
		</action>


	</package>




</struts>

package www.csdn.project.action;

import www.csdn.project.dao.AdminDAO;
import www.csdn.project.dao.AdminDAOImpl;
import www.csdn.project.domain.Admin;
import www.csdn.project.service.AdminService;
import www.csdn.project.service.AdminServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {

	private String adminName;
	private String adminPassword;
	private Admin entity;

	private AdminService adminService = new AdminServiceImpl();

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}

	public Admin getEntity() {
		return entity;
	}

	public String login() {
		entity = adminService.login(adminName, adminPassword);
		if (entity != null) {
			return SUCCESS;
		} else {
			return LOGIN;
		}
	}
	
	public String insert() {
		boolean b = false;
		b = adminService.add(entity);
		
		if(b) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}


}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3>
				后台管理登陆界面
			</h3>

			<hr />

			<form
				action="${pageContext.request.contextPath}/csdn/adminLogin.action"
				method="post">
				用户名:
				<input type="text" name="adminName" />
				<br />
				密  码:
				<input type="password" name="adminPassword" />
				<br />
				<input type="submit" value="Login" />

			</form>
		</div>
	</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3> 
				欢迎${entity.adminName}后台管理界面! 
			</h3><br><br>
			
		</div>
	</body>
</html>


下面的图说明了location的来历:

Redirect类型
(1)、 说明

Redirect属于重定向。如果用redirect类型,则在reuqest作用域的值不能传递到前台。

(2)、 例子

页面参照:/manager/index.jsp

Action参照:AdminAction

配置文件: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>




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


		<action name="adminLogin" class="www.csdn.project.action.AdminAction"
			method="login">


			<!--第一种写法
			
				<result name="success" type="redirect">/manager/index.jsp</result>
			-->
			<!-- 第二种写法 -->
			<result name="success" type="redirect">
				<param name="location">/manager/index.jsp</param>
			</result>
			<result name="login">/index.jsp</result>
		</action>


	</package>


</struts>

package www.csdn.project.action;

import www.csdn.project.dao.AdminDAO;
import www.csdn.project.dao.AdminDAOImpl;
import www.csdn.project.domain.Admin;
import www.csdn.project.service.AdminService;
import www.csdn.project.service.AdminServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {

	private String adminName;
	private String adminPassword;
	private Admin entity;

	private AdminService adminService = new AdminServiceImpl();

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}

	public Admin getEntity() {
		return entity;
	}

	public String login() {
		entity = adminService.login(adminName, adminPassword);
		if (entity != null) {
			return SUCCESS;
		} else {
			return LOGIN;
		}
	}
	
	public String insert() {
		boolean b = false;
		b = adminService.add(entity);
		
		if(b) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}


}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3>
				后台管理登陆界面
			</h3>

			<hr />

			<form
				action="${pageContext.request.contextPath}/csdn/adminLogin.action"
				method="post">
				用户名:
				<input type="text" name="adminName" />
				<br />
				密  码:
				<input type="password" name="adminPassword" />
				<br />
				<input type="submit" value="Login" />

			</form>
		</div>
	</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3> 
				欢迎${entity.adminName}后台管理界面! 
			</h3><br><br>
			
		</div>
	</body>
</html>




redirectAction类型

(1)、说明

1、 把结果类型重新定向到action

2、 可以接受两种参数

a) actionName: action的名字

b) namespace:命名空间

(2)、 例子

在配置文件中

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




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


		<action name="adminLogin" class="www.csdn.project.action.AdminAction"
			method="login">


			<!--
				<result name="success" type="redirectAction"> <param
				name="actionName">delete.action</param> <param
				name="namespace">/csdn</param> </result>
			-->


			<result name="success" type="redirectAction">csdn/delete.action?name=${entity.adminName}
			</result>


			<result name="login">/index.jsp</result>


		</action>


		<action name="delete"
			class="www.csdn.project.actionreslut.test.action.DeleteAction"
			method="delete">
			<result>/index.jsp</result>
		</action>


	</package>




</struts>

package www.csdn.project.action;

import www.csdn.project.dao.AdminDAO;
import www.csdn.project.dao.AdminDAOImpl;
import www.csdn.project.domain.Admin;
import www.csdn.project.service.AdminService;
import www.csdn.project.service.AdminServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {

	private String adminName;
	private String adminPassword;
	private Admin entity;

	private AdminService adminService = new AdminServiceImpl();

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}

	public Admin getEntity() {
		return entity;
	}

	public String login() {
		entity = adminService.login(adminName, adminPassword);
		if (entity != null) {
			return SUCCESS;
		} else {
			return LOGIN;
		}
	}
	
	public String insert() {
		boolean b = false;
		b = adminService.add(entity);
		
		if(b) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}


}

package www.csdn.project.actionreslut.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class DeleteAction extends ActionSupport {
	
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public String delete(){
		
		System.out.println("删除成功......");
		
		return SUCCESS;
	}

}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3>
				后台管理登陆界面
			</h3>

			<hr />

			<form
				action="${pageContext.request.contextPath}/csdn/adminLogin.action"
				method="post">
				用户名:
				<input type="text" name="adminName" />
				<br />
				密  码:
				<input type="password" name="adminPassword" />
				<br />
				<input type="submit" value="Login" />

			</form>
		</div>
	</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<div align="center">
			<h3> 
				欢迎${entity.adminName}后台管理界面! 
			</h3><br><br>
			
		</div>
	</body>
</html>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值