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

Struts2之数据标签(二)

时间:2017-08-16 21:55:12      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:系统   国际   etag   标签   string   上下文   代码   年龄   details   

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之数据标签(二)

标签:系统   国际   etag   标签   string   上下文   代码   年龄   details   

原文地址:http://www.cnblogs.com/claireyuancy/p/7375780.html

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