zoukankan      html  css  js  c++  java
  • Struct2(三) Struct2 标签

        在上一篇 Struct2(二)中,我们新建了工程Struct2test用来验证hello World 程序,在index.jsp中,我们添加了一个Struct2 uri 标签用来创建一个指向hello.action 的超链接,这篇是更加详细的介绍Struct2 的标签。

    在创建动态的响应上,web应用程序,不同于传统的应用程序。为了更加简单的引用一个页面的动态数据,Struct2 框架提供了一套标签。一些标签模仿标准的HTML标记,同时提供了扩展。其他标签不是标准的标签,但能提供比较有用的控制。

    注意:

    为了使用Struct2 标签集,你必须包括一个标签库命令,一般就是:

    <%@ taglib prefix="s" uri="/struts-tags" %>.

    1. Struct2 的url标签

    Struct2 标签其中的一个就是创建一个指向其他web资源的链接,特别是指向本地应用程序中的资源。

    注意:

    HTML中提供一个标签a来创建超链接,HTML标签常常要求冗余的信息,并且不是特别方便的能够访问到框架提供的信息。

    这个标签一个比较常用的功能是:链接到其他的页面,在hello World 例子中 index.jsp 中hello.action 就是使用了一个Struct2 url 标签,想了解更多关于url标签的,见 url documentation

    例子: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>

    与之相对应的是:struts.xml

    <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">

    <result name="success">/HelloWorld.jsp</result>

    </action>

    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>

    嵌套在标签里面的叫做参数标签,这个标签可以让我们指定一个参数

    标签的生命为var类型的名称,然后能够使用这个变量为${名称},免除重复定义。

    2. Struts 2 Form Tags

    例子:

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

    <s:form> 为定义一个表单

    <s:textfield> html的输入的标签

    <s: submit> 提交按钮

    When the index page is return by the server to the browser you should see:

    转为为html为:

    <form id="hello" name="hello" action="/Using_Tags_Struts2_Mvn/hello.action;jsessionid=3471d76027b5342cab44f297b567" method="post">
    
    <table class="wwFormTable">
    
    <tr>
    
    <td class="tdLabel"><label for="hello_userName" class="label">Your name:</label></td>
    
    <td><input type="text" name="userName" value="" id="hello_userName"/></td>
    
    </tr>
    
    <tr>
    
    <td colspan="2"><div align="right"><input type="submit" id="hello_0" value="Submit"/>
    
    </div></td>
    
    </tr>
    
    </table>
    
    </form>
    3. Struts 2 property tag

    <s:property value="messageStore.message" />

    修改我们上一个例子,来测试这篇的标签

    在HelloWorldAction中加入:

    private static int helloCount = 0;

    public int getHelloCount() {

    return helloCount;

    }

    public void setHelloCount(int helloCount) {

    HelloWorldAction.helloCount = helloCount;

    }

    记录Action执行的次数,相对应的在execute方法中,加入:

    helloCount++;

    每次点击超链接的时候,就会增加。

    显示点击的次数:

    <p>I've said hello <s:property value="helloCount" /> times!</p>

    在MessageStore 中增加:

    public String toString(){
           return message +"from to String.";
         }

    展示的效果:

    <p><s:property value="messageStore" /></p>

    最后的HelloWorld.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>Hello World!</title>
      </head>
      <body>
        <p>I've said hello <s:property value="helloCount" /> times!</p>
        <p><s:property value="messageStore" /></p>
        <h2><s:property value="messageStore.message" /></h2>
      </body>
    </html>

    最终的效果:

    image

    上面只是Struct2 几个简单的标签,更多的标签见:Struts 2 Tag Reference

  • 相关阅读:
    大话设计模式C++ 适配器模式
    36. Valid Sudoku
    c++ 常用的数据结构
    《大话设计模式》c++实现 状态模式
    c++ 各种类型转换
    38. Count and Say
    《大话设计模式》c++实现 抽象工厂模式
    66. Plus One
    49. Group Anagrams
    《大话设计模式》c++实现 建造者模式
  • 原文地址:https://www.cnblogs.com/zhailzh/p/3988385.html
Copyright © 2011-2022 走看看