Struts2中的链接标签 <s:url>和<s:a>
普通链接
Web程序中最普通的应用是链接到其他页面,下面看Welcome.jsp。
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
   <title>Welcome</title>
   <link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet"
         type="text/css"/>
</head>
 
<body>
<h3>Commands</h3>
<ul>
   <li><a href="<s:url action="Login_input"/>">Sign On</a></li>
   <li><a href="<s:url action="Register"/>">Register</a></li>
</ul>
 
</body>
</html>
 
1.1说明
1.<%@ taglib prefix="s" uri="/struts-tags" %>
此句表示导入struts标签,并以s为前缀。即以s为前缀的标签均来自struts标签库。
 
2.<link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet" type="text/css"/>
此句表示利用url标签导入一个路径,链接到一个文件,注意此路径为项目下的绝对路径。
 
3.<a href="<s:url action="Login_input"/>">Sign On</a>
此句表示利用url标签链接到一个action。
 
1.2注册action
我们在struts.xml中注册一个action来显示welcome.jsp。
<action name="Welcome">
      <result>/example/Welcome.jsp</result>
</action>
注意此action注册在package example下,所以在地址栏中敲入http://localhost:8080/StrutsHelloWorld/example/Welcome.action(StrutsHelloWorld是project名),会导向到Welcome.jsp。
 
 
2.使用通配符
对于上面的action注册,我们也可以用下面的语句代替。
<action name="*">
      <result>/example/{1}.jsp</result>
</action>
此句的意思是,如果在没有找到匹配的action名称的情况下,默认调用action名称.jsp。第一句中星号指任意,而第二句中{1}指代第一句中星号指代的内容。
    举个例子,如果在地址栏中敲入http://localhost:8080/StrutsHelloWorld/example/1.action,则系统查找struts.xml,发现没有name为1的action,即最后调用name为星号的这个action,根据此action,将输出/example/1.jsp。
或者读者可以直接点击Welcome.jsp中的两个超链接,系统将会报错找不到Login_input.jsp和Register.jsp。因为这两个action还没有注册,也没有相应的jsp文件。
 
3.带参数的链接
超链接后面带有参数大家不会陌生,诸如http://www.apache.com/?language=ch。这个链接后面带有一个language参数,其值为ch。你可以通过request.getParameter(“language”)找到参数值。下面演示在struts2中如何设置带参数的链接。看HelloWorld.jsp。
<%@ taglib prefix="s" uri="/struts-tags"%>
 
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
<h3>Languages</h3>
<ul>
      <li>
      <s:url id="url" action="HelloWorld">
             <s:param name="request_locale">en</s:param>
      </s:url>
      <s:a href="%{url}">English</s:a>
      </li>
      <li>
      <s:url id="url" action="HelloWorld">
             <s:param name="request_locale">es</s:param>
      </s:url>
      <s:a href="%{url}">Espanol</s:a>
      </li>
</ul>
 
</body>
</html>
3.1说明
1.<s:url id="url" action="HelloWorld">
             <s:param name="request_locale">en</s:param>
</s:url>
此段表示设置一个url标签指向名为HelloWorld的action,此标签带一个id取名为url,后面会用到。带一个参数request_locale,其值为en。
 
2.<s:a href="%{url}">English</s:a>
此句用到了struts2的超链接标签,连接的地址即为1中url,点击English,发出的信息为:http://localhost:8080/StrutsHelloWorld/example/HelloWorld.action?request_locale=en
 
 
3.2注册action到struts.xml
<struts>
      <package name="example" namespace="/example"
             extends="struts-default">
             <action name="HelloWorld" >
                    <result>/example/HelloWorld.jsp</result>
             </action>
       </package>
</struts>

struts2 标签 --<<s:url >的更多相关文章

  1. struts2<s:property />标签

    struts2的<property />标签是输出标签 其value属性是指定输出的内容,如果value属性没有写出来,则默认输出对象栈栈顶的元素. 例如,我们在对象栈中添加一个Perso ...

  2. struts2 <s:property/>标签的使用--输出时间格式转换

    转载地址http://hi.baidu.com/nonyi_com/blog/item/acf1b8d74b6cf63e07088bc4.html 最近在使用struts2的<s:propert ...

  3. [Struts2] <s:property>标签

    调用类中的属性,通过get方法调用,而非其属性名调用. 比如A类中有属性a1,a1的get方法是getA1Method(); 相应Action类中的get方法是getA1Action(). 那么需要通 ...

  4. struts2使用jsp和<s:property>标签获取json格式的返回数据

    struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...

  5. struts2 s:if标签以及 #,%{},%{#}的使用方法

    <s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...

  6. struts2 s:if标签以及 #,%{},%{#}的使用方法等在资料整理

    <s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...

  7. Struts2的OGNL标签详解

    一.Struts2可以将所有标签分成3类: UI标签:主要用于生成HTML元素的标签. 非UI标签:主要用于数据库访问,逻辑控制等标签. Ajax标签:用于Ajax支持的标签. 对于UI标签,则有可以 ...

  8. struts2.0 s标签_小小鸟_百度空间

    struts2.0 s标签 1. Struts2页面开发中常用标签使用说明 1.1.往action里传值的使用方式: <input name="userName" type= ...

  9. struts2:数据标签

    目录 数据标签1. a标签2. set标签3. push标签4. bean/param标签5. date标签6. include标签7. url标签8. property标签9. debug标签10. ...

  10. Struts2之数据标签(二)

    Struts2之数据标签(一):http://blog.csdn.net/u012561176/article/details/46848817 1.action标签:使用此标签能够同意在JSP页面中 ...

随机推荐

  1. 轻量级应用开发之(02)UIView

    一 控件 1.屏幕上的所有UI元素都叫做控件(也有叫做视图.组件)比如按钮(UIButton).文本(UILabel)都是控件. 2.控件的共同属性有哪些? 尺寸,位置,背景色 3. 苹果将控件的共同 ...

  2. HTTP负载测试——Tsung

    参考资料:http://blog.jobbole.com/87509/ 如何生成每秒百万级别的 HTTP 请求? 在进行负责测试时要牢记一件重要的事:你能在 Linux 上建立多少个 socket 连 ...

  3. CodeForces 701A Cards

    直接看示例输入输出+提示 1. 统计所有数的和 sum,然后求 sum/(n/2) 的到一半数的平均值 6 1 5 7 4 4 3 ->1+5+7+4+4+3=24  分成3组 每组值为8 in ...

  4. spring mvc静态资源文件的引用

    在页面的<title>下 <link rel="stylesheet" href="<%=request.getContextPath()%> ...

  5. WPF TreeView绑定字典集合

    <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...

  6. 实现JavaScript自定义函数的整合、链式调用及类的封装

    函数声明形式:表单验证函数 1 2 3 4 5 6 7 8 9 10 11 12 13 function checkName(){     console.log('检查用户名'); } functi ...

  7. CentOS6.5安装iftop

    iftop这个小工具是Linux和unix下的top命令升级版,功能相对较强,界面易懂.今天安装了CentOS6.5的最新版,装个小工具检查下系统运行性能. 官网:http://www.ex-parr ...

  8. Button控件常用api

    加载按钮的纹理.loadTextures (const std::string &normal, const std::string &selected, const std::str ...

  9. 使用mysql服务来记录用户的反馈

    经过前几篇教程的学习,相信你对于微信的操作与SAE和webpy都有了些了解,那么这次我想加一个功能,通过mysql来记录用户的反馈,如用户输入fk+内容,然后通过一个页面来显示,最终的效果如下 htt ...

  10. [Effective JavaScript笔记]第3条:当心隐式的强制转换

    js对类型错误出奇的宽容 3+true;  //4 3*””;  //0 3+[]; //3 3+[3]; //33 以上表达式在许多语言早就变红了.而js不但不报错还给你个结果. 极少情况会产生即时 ...