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)";
         
这时候可以在HelloWorld.jsp中添加如下代码:

<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.


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 除皱针眉毛上扬怎么办 玩游戏充钱后悔怎么办 我被欺骗了感情怎么办 有隐私被威胁了怎么办 在微信上被威胁怎么办 别人恐吓我我该怎么办 警属受到威胁该怎么办 出了事故家属闹怎么办 孩子不愿和人交流怎么办 4岁双胞胎说话迟怎么办 三岁宝宝说话迟怎么办 3岁幼儿说话迟钝怎么办 人很迷茫的时候怎么办 人在迷茫的时候怎么办 缺乏安全安感该怎么办 皮球掉进树洞里怎么办 遇到不会读的字怎么办 机票行程单假的怎么办 老婆失忆了怎么办微盘 精神差总想睡觉怎么办 孕妇查溶血值256怎么办 饿了怎么办txt百度云 小猫喜欢和人睡怎么办 房子卖了后户口怎么办 若房子卖了户口怎么办 我房子卖了户口怎么办 雄安房子不卖怎么办 擦伤后留下红印怎么办 膝盖摔伤了破皮怎么办 膝盖擦破皮肿了怎么办 跪久了膝盖疼怎么办 摔倒了膝盖肿了怎么办 膝盖摔伤了肿了怎么办 膝盖破皮流血了怎么办 擦伤化脓又肿了怎么办 摔倒了膝盖破皮怎么办 膝盖擦伤碰水了怎么办 收脚运动裤长了怎么办 1岁宝宝发烧反复怎么办 3岁宝宝反复发烧怎么办 3岁宝宝咳嗽发烧怎么办