Struts2_19_DynamicResult

来源:互联网 发布:几米软件 编辑:程序博客网 时间:2024/06/06 06:55
动态结果集:
action中的result页面是动态获取的,即通过属性的改变,虽然Result name是一样,但是结果页面不一样。
 
重点:
<?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 name="struts.i18n.encoding" value="GBK"></constant>
     <package name="user" namespace="/user" extends="struts-default">
         <action name="user" class="com.awei.struts2.action.UserAction">
             <result >${r}</result>
         </action>
    </package>
    <!-- Add packages here -->
</struts>
 
这个r是什么呢?
来看com.awei.struts2.action.UserAction的定义:
 
package com.awei.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
    private String type;
    private String r;
    public String getR() {
        return r;
    }
    public void setR(String r) {
        this.r = r;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String execute(){
        if("1".equals(type)){
            r="/index1.jsp";
        }else if("2".equals(type)){
            r="/index2.jsp";
        }
        return SUCCESS;
    }
    
}
 
Action会把所有的属性都加入到value Stack中,这样就可以通过Property Name来获得value;
${r}是OGNL表达式。
 
index.jsp页面的定义:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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/csshref="styles.css">
    -->
  </head>
  
  <body>
    <a href="user/user?type=1">进入界面一</a> <br>
    <a href="user/user?type=2">进入界面二</a> <br>
  </body>
</html>
0 0
原创粉丝点击