首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

s:url标签应用

2012-10-09 
s:url标签使用一.?????value?? s:set namemyurl valuehttp://www.baidu.com/s:set?? ???????

s:url标签使用

一.?????value?

? <s:set name="myurl" value="'http://www.baidu.com'"></s:set>??
??????? value以字符处理:?? <s:url value="#myurl"></s:url><br>??
??????? value明确指定以ognl表达式处理:??? <s:url value="%{#myurl}"></s:url>??

二.??? action

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

?

?

?

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=en3.2注册action到struts.xml<struts>??????<package name="example" namespace="/example"?????????????extends="struts-default">?????? ??????<action name="HelloWorld" >????????????????????<result>/example/HelloWorld.jsp</result>
?????????????</action>
?????? </package>
</struts>

热点排行