课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Struts2 include标签

当前位置:免费教程 » Java相关 » Struts2
Struts的include标签非常类似于jsp的include标签,它很少被使用。我们已经看到了如何使用<s:action>标签将struts action的输出包含到jsp中。而<s:include>标签略有不同,它允许你将jsp,servlet或任何其他资源(除了struts action之外的其他资源)的输出包含到jsp中。在幕后,它完全类似于<jsp:include>,但它允许你传递参数到包含的文件中,并且它也是Struts框架的一部分。
下面的示例显示了我们如何将HelloWorld.jsp的输出包含到employee.jsp中。在这种情况下,HelloWorldAction.java中的action方法将不会被调用,因为是直接包含了jsp。

创建action类

  1. package com.w3xue.struts2;
  2. public class HelloWorldAction{
  3. private String name;
  4. public String execute() throws Exception {
  5. return "success";
  6. }
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. }

创建视图

让我们创建包含以下内容的HelloWorld.jsp
  1. <%@ page contentType="text/html; charset=UTF-8" %>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <html>
  4. <head>
  5. <title>Hello World</title>
  6. </head>
  7. <body>
  8. <h2>Example of Generator Tag</h2>
  9. <h3>The colours of rainbow:</h3>
  10. <s:generator val="%{'Violet,Indigo,Blue,
  11. Green,Yellow,Orange,Red '}" count="7"
  12. separator=",">
  13. <s:iterator>
  14. <s:property /><br/>
  15. </s:iterator>
  16. </s:generator>
  17. </body>
  18. </html>
接下来创建有以下内容的employees.jsp
  1. <%@ page contentType="text/html; charset=UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4. <head>
  5. <title>Employees</title>
  6. </head>
  7. <body>
  8. <p>An example of the include tag: </p>
  9. <s:include value="HelloWorld.jsp"/>
  10. </body>
  11. </html>

配置文件

你的struts.xml应该如下所示:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true" />
  7. <package name="helloworld" extends="struts-default">
  8. <action name="hello"
  9. class="com.w3xue.struts2.HelloWorldAction"
  10. method="execute">
  11. <result name="success">/HelloWorld.jsp</result>
  12. </action>
  13. <action name="employee"
  14. class="com.w3xue.struts2.Employee"
  15. method="execute">
  16. <result name="success">/employee.jsp</result>
  17. </action>
  18. </package>
  19. </struts>
你的web.xml应如下所示:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <display-name>Struts 2</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <filter-name>struts2</filter-name>
  14. <filter-class>
  15. org.apache.struts2.dispatcher.FilterDispatcher
  16. </filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>struts2</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>
右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/employee.action,将显示以下界面:
系统信息
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号