Struts2-第二篇-Using Struts 2 Tags
来源:互联网 发布:c 结构体中定义数组 编辑:程序博客网 时间:2024/05/18 03:55
在上一篇Helloworld篇中,我们添加index.jsp文件中用到struts2 url tag标签去创建一个超级链接hello.action(<s:url action='hello'''/>).接下来我们继续探究url和其他struts2 tag。
Web应用与传统的web站点不同的是,web应用能够创建一个动态的响应。目的是使用引用动态数据更容易从一个page,struts提供一系列tags。一些标签和html很类似,其他标签非标准的,但是很有效的控制。
Struts2标签的作用之一就是去创建链接到其他web资源,特别是在同一个应用中的其他资源。
就像在HelloWorld篇幅中index.jsp中的链接一样:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Basic Struts 2 Application - Welcome</title></head><body><h1>Welcome To Struts 2!</h1><p><a href="<s:url action='hello'/>">Hello World</a></p></body></html>
当你运行HelloWorld项目是,鼠标点击超级链接是,你看到URL是"hello.action".再检查struts.xml配置
...<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result></action>上面的action node映射hello.action去执行HelloWorldAction类中的方法execute。如果方法返回字符串success,页面view HelloWorld.jsp将返回给用户。
在index.jsp后添加如下带参数的url请求代码:
<s:url action="hello" var="helloLink"> <s:param name="userName">Bruce Phillips</s:param></s:url> <p><a href="${helloLink}">Hello Bruce Phillips</a></p>
相比较放把值在tag's href attribute,上面把s:url标签和实际的代码块分开。内嵌在url标签的是struts2 param tag.这个标签让你指定一个参数名和参数值。
注意变量属性var="helloLink"的用处。var属性值是一个引用,后面我们能用在我们代码中去引用url.
注意上面的锚点,查看href的属性值是${helloLink}.这个页面将替代我们创建的超级链接,查询字符串参数将是properly url-encoded.
Struts 2 Form Tags
大部分的应用有几种数据输入形式。struts2 tag使输入更容易创建。
在index.jsp中,添加
<p>Get your own personal hello by filling out and submitting this form.</p> <s:form action="hello"> <s:textfield name="userName" label="Your name" /> <s:submit value="Submit" /> </s:form>
打开浏览器:http://localhost:8080/Using_Tags_Struts2_Mvn/index.action 注意url中,index.action,不要写成index.jsp,因为struts.xml配置文件中都是index.如下:
<action name="index">
<result>/index.jsp</result>
</action>
Struts2 property tag
在HelloWorld中的HelloWorld.jsp添加代码:
<s:property value="messageStore.message"/>
在HelloWorldAction.java中,添加如下代码:
private static int helloCount = 0; public int getHelloCount() { return helloCount;} public void setHelloCount(int helloCount) { HelloWorldAction.helloCount = helloCount;}
Each time the execute method is called we'll increase helloCount by 1. So add this code to the execute method of class HelloWorldAction.
helloCount++;
在helloworld.jsp中添加:
<
p
>I've said hello <
s:property
value
=
"helloCount"
/> times!</
p
>
在MessageStore中添加:
public
String toString() {
return
message +
" (from toString)"
;
}
<
p
><
s:property
value
=
"messageStore"
/></
p
> 实现的效果和messageStore.message一样。
Since getMessageStore of HelloWorldAction class returns a MessageStore object, Struts 2 will call the toString method of class MessageStore. The string returned by that toString method will be displayed in the browser.
- Struts2-第二篇-Using Struts 2 Tags
- Using Struts 2 Tags
- Struts2 官方教程之Using Struts 2 Tags (五)
- struts2标签的使用(Using Struts 2 Tags)
- Struts2教程三——Using Struts 2 Tags
- 2.Using Struts 2 Tags
- Struts2标签错误:using Struts tags without the associat解决
- Struts2--struts-tags
- Struts2--struts-tags (转)
- Struts2---Struts-Tags
- struts2的struts-tags标签
- struts2 学习总结 ---- Struts-tags
- struts2 配置出现错误信息This is usually caused by using Struts tags without the associated filter
- Struts 2 Data Tags
- Struts 2 Control Tags
- Struts 2 Data Tags
- struts2标签定义文件 struts-tags-tld
- struts2-jquery, struts-jquery-grid-tags
- gcc/g++编译优化
- Android MTK平台最完备的开机动画修改教程
- 2.linux如何学习
- 记录几个Android上利用Camera传输视频流的开源例子
- linux 识别目录和文件
- Struts2-第二篇-Using Struts 2 Tags
- 模板方法模式
- Notepad++的字体设置加Consolas和微软雅黑混合字体
- Spring data jpa的OpenEntityManagerInViewFilter
- cantor数,程序不难,但规律很难找到!!!
- 真机使用Hierarchy工具调试UI
- 算法导论学习-----霍纳规则
- 对《java程序员上班那点事》笔者对数组占用内存质疑
- android 短信监听