码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2之数据标签(二)

时间:2015-07-13 22:30:50      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:struts2   action标签与include标签   url标签i18n与text标签   date标签   debug标签   

Struts2之数据标签(一):http://blog.csdn.net/u012561176/article/details/46848817

1.action标签:使用此标签可以允许在JSP页面中直接调用Action,因为需要调用Action,故可以指定需要被调用Action的name以及namespace。如果指定了executeResult参数的属性值为true,该标签还会把Action的处理结果(视图支援)包含到本页面中来。

(1).此标签的属性:

— id:可选属性,该属性将会作为该Action的引用ID。

— name:必填属性,指定该标签调用哪个Action。

— namespace:可选,指定该标签调用的Action所在的namespace(命名空间)。

— executeResult:可选,指定是否要将Action的处理结果页面包含到本页面。默认值为false,即不把Action的处理结果包含到本页面。

— ignoreContextParams:可选,指定该页面中的请求参数是否需要传入调用的Action,默认是false,即将本页的请求参数传入被调用的Action。

(2).接下来附上例子,新建一个Struts2项目,项目名为DataLabelTest2.

— 新建一个Action类,类名为PersonAction,代码如下:

package com.action;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String execute() {
		return SUCCESS;
	}
	
	public String welcome(){
		ServletActionContext.getRequest().setAttribute("welcome", "欢迎来到人类世界");
		return SUCCESS;
	}
}

— 接着配置一下struts.xml配置文件,代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
        
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<action name="person" class="com.action.PersonAction">
			<result>/success.jsp</result>
		</action>
		<action name="welcome" class="com.action.PersonAction" method="welcome">
			<result>/welcome_success.jsp</result>
		</action>
	</package>
</struts>


— 接着新建两个JSP文件,分别为success.jsp和welcome_success.jsp页面,代码如下:

success.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 '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> 
    姓名:<s:property value="name"/><br/>
    年龄:<s:property value="age"/><br/>
  </body>
</html>

welcome_sccess.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'welcome_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>
    <h3>执行结果,并将结果页面的输出包含到本页面中</h3>
    <s:action name="person" namespace="/" executeResult="true"/>
    <h3>不执行结果,调用PersonAction的welcome()方法,获取请求对象中的welcome属性</h3>
    <s:action name="welcome" namespace="/" executeResult="false"/>
    <s:property value="#attr.welcome"/>
    <h3>执行结果,并通过嵌套的param标签,设置PersonAction的name和age属性</h3>
    <s:action name="person" namespace="/" executeResult="true">
    	<s:param name="name" value="'王五'"></s:param>
    	<s:param name="age" value="30"></s:param>
    </s:action>
  </body>
</html>


 — 最后,运行效果如下:

技术分享



2.include标签:用于将一个JSP页面,或者一个Servlet包含到本页面中。

(1).此标签具有以下属性:

— value:必填,指定需要被包含的JSP页面或者Servlet。

— id:可选,指定该标签的ID引用。

(2).还可以为此标签指定多个param子标签,用于将多个参数值传入被包含的JSP页面或者Servlet。

(3).接着在上面那个DataLabelTest项目底下,新建一个include.jsp页面,主要代码如下:

    <s:include value="welcome_success.jsp"/>


运行后结果如下:

技术分享

注:通常此标签用于所有JSP页面共有拥有的,如顶部和底部的显示。



3.url标签:用于生成一个URL地址,可以通过param标签向url标签指定参数,从而指定URL发送请求参数。

(1).此标签的属性有:

— includeParams:可选,指定是否包含请求参数。该属性的属性值只能为none,get或者all。

— scheme:可选,用于设置scheme属性。

— value:可选,指定生成URL的地址。如果不提供就用action属性指定的Action作为URL地址值。

— action:可选,指定生成URL的地址为哪个Action,如果Action不提供,就使用value属性作为URL的地址值。

— namespace:可选,该属性指定命名空间。

— method:可选,指定使用Action的方法。

— encode:可选,指定是否需要encode请求参数。

— includeContext:可选,指定是否需要将当前上下文包含在URL地址中。

— anchor:可选,指定URL的描点。

— id:可选,指定该url元素的引用id。

 (2).其中action属性和value属性的作用大致相同。指定action属性,系统会在指定属性后加.action后缀。如果两个都没有指定,就以当前页作为URL的地址值。

(3).我新建一个url.jsp页面,主要代码为:

<s:url action="person"/>

运行后效果如下:

 技术分享



4.i18n标签和text标签:这两个标签用于对国际化提供支持.i18n标签,用于将一个资源包放入值栈中,text标签用于从资源包中获取消息。

(1).i18n标签的属性:name:指定使用的资源包的名称,如 /foo/bar/customBundle

(2).text标签的属性:name:指定使用的资源文件的属性名称。id:指定该text标签的引用ID。



5.date标签:用于格式化输入的一个日期,还可以计算指定日期和当前时刻之间的时差。

(1).此标签的属性有:

— format:可选,如指定该属性,将根据该属性指定的格式来格式化日期。

— nice:可选,值为true或者false,用于指定是否输出指定日期和当前时刻的时差。默认为false,即不输出。

— name:必填,指定要格式化的日期。

— id:可选,指定引用该元素的id值。

(2).通常,nice属性和format属性不同时指定,不指定nice属性时,该属性值为false。因为指定nice属性值为true的话,代表输出指定日期和当前时刻的时差;指定format属性,则表明将指定日期按format指定的格式来个格式化输出。

(3).如果即没有指定format属性,也没指定nice的属性值为true时,则系统会到国际化资源文件中寻找key为struts.date.format的消息,将该消息当成格式化文本来格式化日期。如果无法找到key为struts.date.format的消息,则默认采用DateFormat.MEDIUM格式输出。

(4).在DataLabelTest2项目底下,新建一个date.jsp页面,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	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 'date.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>
	<%
		Date date = new Date();
		pageContext.setAttribute("date", date);
	%>
	<!-- nice="false",且指定format="yyyy/MM/dd" -->
	<s:date name="#attr.date"  format="yyyy/MM/dd" nice="false" /><br/>
	<!-- nice="true",且指定format="dd/MM/yyyy" -->
	<s:date name="#attr.date" format="dd/MM/yyyy" nice="true"/><br/>
	<!-- 仅指定nice="true" -->
	<s:date name="#attr.date" nice="true"/><br/>
	<!-- 仅指定nice="false" -->
	<s:date name="#attr.date" nice="false"/><br/>
	<!-- 仅指定format="yyyy-MM-dd" -->
	<s:date name="#attr.date" format="yyyy-MM-dd"/><br/>
</body>
</html>

最后,运行效果如下:

技术分享



6.debug标签:用于在页面上生成一个调试链接,当单击该链接时,可以看到当前ValueStack和Stack Context中的内容。

主要代码如下:

	<s:debug></s:debug>


运行效果如下:

技术分享




7.以上就是Struts2数据标签中的一些标签,写得不好,请见谅,如有错误,请指出,谢谢!



版权声明:本文为博主原创文章,未经博主允许不得转载。

Struts2之数据标签(二)

标签:struts2   action标签与include标签   url标签i18n与text标签   date标签   debug标签   

原文地址:http://blog.csdn.net/u012561176/article/details/46860737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!