普通链接
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>
分享到:
相关推荐
1. <s:generator separator="" val=""></s:generator>----和<s:iterator>标签一起使用 H: 1. <s:head/>-----在<head></head>里使用,表示头文件结束 2. <s:hidden></s:hidden>-----隐藏值 I: 1. <s:...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>标签 16.4.6、<html:hidden...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>标签 16.4.6、<html:hidden...
3、如果需要后台java代码获取内容,可以在action中定义content属性,getContent就可以获取到 需要注意的是,这里的 <jsp:include page="/kindEditor/jsp/test.jsp"> <jsp:param name="formName" value="Form3"/> ...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<...
16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...
2. 在 JSP 页面中,使用表单或其他方式将参数传递给 Action。 3. 在 Action 中,使用设定的属性接收参数,例如 `userName`。 例如,在 Action 中定义了 `userName` 属性,可以在 JSP 页面中使用 `user1!add?...
<html:form> <html:form>用来创建表单,<html:form>必须包含一个action属性,否则JSP会抛出一个异常.
Struts原理、开发及项目实施<br/> Holen 2002-9-12<br/><br/>1、 摘要<br/>2、 关键词<br/>3、 Framework<br/>4、 Struts的起源<br/>5、 Struts工作原理<br/>6、 Struts安装<br/>7、 一个实例<br/>8、 Struts优缺点...
next2: JSP服务器容器把<servlet-mapping>的HelloWorld.do指到<action-mappings>的path="/HelloWorld"; next3: struts控制器根据name="HelloForm"找<form-beans>中对应的<form-bean> next4: <form-bean>调用指定的...
10.2.2 <decorator:usePage>、<decorator:useHtmlPage>和<decorator:head>标签的使用介绍 200 附录 204 附录1 安装JBoss让它部署Web项目 204 F1.1 安装 204 F1.2 手工部署Web项目 207 附录2 Mysql数据库使用和创建...
2、复制struts需要的jar包和kindEditor必须jar包到lib下,因为它们的jar包邮重复,选择最新的就ok 3、如果需要后台java代码获取内容,可以在action中定义content属性,getContent就可以获取到 需要注意的是,这里的 ...
首先需要在提交的jsp页面(要使用token机制,必须使用struts2提供的标签库)加上 <s></s> 这段代码,然后在struts.xml里面需要进行如下配置: <action name=token class=com.xiaoluo.struts2.TokenAction> ...
<result name="IOException" >/error/ioexception.jsp</result> <result name="SQLException" >/error/sqlexception.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result=...
第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...
<url-pattern>/*</url-pattern> </filter-mapping> 其中, encoding 指定了字符集为GBK,forceEncoding 指定了是否强制使用该过滤器的字符集。 二、 Struts.properties 配置 在struts.properties文件中添加...