现在的位置: 首页 > 综合 > 正文

Struts2 学习笔记 11 Result part2

2018年04月04日 ⁄ 综合 ⁄ 共 2654字 ⁄ 字号 评论关闭

  之前学习了result type 和global result 我们现在来说一下

1.Dynamic Result动态结果集。先来看一下小项目的目录



首页的两个链接访问user/user?type=1 和user/user?type=2。来看一下struts.xml的配置

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="/user" extends="struts-default">
    
		<action name="user" class="com.tfj.struts2.action.UserAction">
			<result>${r}
			</result>
		</action>
       
    </package>

</struts>

这里的<result>${r}</result>就是一个动态结果集,那么是怎么调用的呢。在UserAction.java里定义了r字符串变量,实现get() set() 方法并给r复制来实现动态调用。

UserAction.java

package com.tfj.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private String r;
	public String getR() {
		return r;
	}
	public void setR(String r) {
		this.r = r;
	}
	private int type;
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	@Override
	
	public String execute() throws Exception {
		if(type==1)  r="/success.jsp";
		else if(type==2)  r="/error.jsp";
	 return "success";
	}
	
}

定义的r也成为action的一个属性,我们可以用debug查看到。

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" />
    <package name="default" namespace="/user" extends="struts-default">
    
		<action name="user" class="com.tfj.struts2.action.UserAction">
			<result type="redirect">
			/success.jsp?t=${type}
			</result>
		</action>
       
    </package>

</struts>

result类型是redirect,传递的来type=1,我们要在success.jsp里接受type的值。我们看一下结果如下

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags"  prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.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>
    Success. <br>
    from valuestack: <s:property value="t"/><br>
    from actioncontext: <s:property value="#parameters.t"/>
    <s:debug></s:debug>
  </body>
</html>


注意这里<s:property value="t"/>拿不到值,只有<s:property value="#parameters.t"/>可以拿到值。因为是redirect,所以值栈为空,debug已经看到了,若是用forward传递共用值栈,不需要这样,redirect只能用第二种方式拿到值。

下图为关系图



资源下载:DynamicResult.rar

   
ResultWithParams.rar
   



抱歉!评论已关闭.