1.下载Struts2开发包:http://struts.apache.org/2.x/index.html;
2. 新建站点,添加Struts2支持:将开发包中apps\struts2-blank-2.1.8.1.war用WinRAR解压
a.将 WEB-INF\classes\struts.xml拷贝到网站项目的src目录下;
b.将WEB-INF\lib中的jar文件拷贝到网站项目的WEB-INF\lib目录下;
c.将WEB-INF\web.xml中<web-app>中的<filter>和<filter-mapping>拷贝到网站项目的<web-app>下;
3.配置 struts.xml,格式如下:
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="cn.edu.ahau.mgc.struts2.action.HelloAction">
<result name="success">/helloWorld.jsp</result>
</action>
</package>
a.<package> 中namespace和<action>中name的组合为action的访问路径,namespace默认为"",本例的访问路径为 “/hello.action”,“.action”可以省略,简写为“/hello”;
b.<action>中的 class为action的类;
c.<result>中一个name对应一个页面,根据action返回的字符串匹配跳转的页面,name默认为“success”;
4.创建action类:
a.创建action类的三种方法:
· 普通Java类,含有public String execute()方法;
·实现Action接口的类;
· 继承ActionSupport的类,最常用的方法,好处是ActionSupport类提供了很多常用的方法;
b.执行action 类,通过execute方法返回的字符串在跳转到struts.xml中相匹配的页面;
5.创建jsp页面。
实例:HelloWorld
web.xml:
01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app version="2.5"
03. xmlns="http://java.sun.com/xml/ns/javaee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
06. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07. <welcome-file-list>
08. <welcome-file>hello.jsp</welcome-file>
09. </welcome-file-list>
10. <filter>
11. <filter-name>struts2</filter-name>
12. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13. </filter>
14. <filter-mapping>
15. <filter-name>struts2</filter-name>
16. <url-pattern>/*</url-pattern>
17. </filter-mapping>
18.</web-app>
-------------------------------------------------------------------------------
struts.xml:
01.<?xml version="1.0" encoding="UTF-8" ?>
02.<!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd">
05.
06.<struts>
07. <!--
08. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
09. <constant name="struts.devMode" value="false" />
10.
11. <include file="example.xml"/>
12.
13.
14.
15. <package name="default" namespace="/" extends="struts-default">
16. <default-action-ref name="index" />
17. <action name="index">
18. <result type="redirectAction">
19. <param name="actionName">HelloWorld</param>
20. <param name="namespace">/example</param>
21. </result>
22. </action>
23. </package>
24. -->
25.
26. <!-- Add packages here -->
27. <constant name="struts.devMode" value="true" />
28. <package name="default" namespace="/" extends="struts-default">
29. <action name="hello" class="cn.edu.ahau.mgc.struts2.action.HelloAction">
30. <result name="success">/helloWorld.jsp</result>
31. </action>
32. </package>
33.</struts>
----------------------------------------------------------------------------
HelloAction.java:
01.package org.shunhui.struts2.action;
02.
03.import com.opensymphony.xwork2.ActionSupport;
04.
05.public class HelloAction extends ActionSupport {
06.
07. @Override
08. public String execute() {
09. return "success";
10. }
11.
12.}
---------------------------------------------------------------------------
hello.jsp:
01.<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09. <head>
10. <base href="<%=basePath%>">
11.
12. <title>Hello,World!</title>
13. <meta http-equiv="pragma" content="no-cache">
14. <meta http-equiv="cache-control" content="no-cache">
15. <meta http-equiv="expires" content="0">
16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17. <meta http-equiv="description" content="This is my page">
18. <!--
19. <link rel="stylesheet" type="text/css" href="styles.css">
20. -->
21. </head>
22.
23. <body>
24. Hello,World! <br>
25. </body>
26.</html>
分享到:
相关推荐
一个完整的struts2框架实现实例,需要的包、配置文件均可参考。
8.1.1 页面上:如何使用自定义结果组件构建Struts 2 Ajax应用程序 171 8.1.2 实现JSON结果类型 173 8.2 常用的结果类型 180 8.2.1 RequestDispatcher,也叫做dispatcher 180 8.2.2 ServletRedirectResult,也叫做...
如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了,所以大家使用新框架开发OLTP(Online Transaction Processing)应用程序时,首先会研究一下如何编写CRUD。这类似于大家在学习新编程语言...
Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放源代码 好了...
8种Java Web框架安装手记,分别是Grails,GWT,JSF,Play,Spring,Struts,Vaadin,Wacket。全部是截止2013-12-19最新的框架版本,文档最后有链接可以下载所有相关资料和jar包以及示例程序,任何问题可以联系...
Struts映射到Model 2设计模式的完整介绍 , 详细论述利用JSP使用Struts的知识,包括Struts标记库 Struts异常处理、验证和登录 Struts应用程序的国际化和本地化 完整论述新StrutsTiles模板库 ...
Struts映射到Model 2设计模式的完整介绍 , 详细论述利用JSP使用Struts的知识,包括Struts标记库 Struts异常处理、验证和登录 Struts应用程序的国际化和本地化 完整论述新StrutsTiles模板库 ...
考虑到ExtJS与主流服务端框架的整合越来越紧密,《ExtJS Web应用程序开发指南(第2版)》还特意讲解了Struts和Struts 2框架的整合与配置。最后全面细致地讲解了ExtJS 4.0中新增的主题、图形和图表部分,真正实现完全...
Spring MVC属于Spring...使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts 2(一般老项目使用)等等。
2.4.2熟悉helloworld框架结构 29 本章小结 32 课后练习 32 第3章jsp和servlet 33 3.1开发第一个jsp+servlet应用 33 3.1.1创建工程 33 3.1.2编写程序 34 3.1.3部署应用 35 3.2认识jsp 36 3.2.1jsp的工作原理...
10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...
10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...
4.1.1 实例:在My Eclipse中编写Helloworld程序 4.1.2 实例:手工编写:Helloworld程序 4.2 Servlet基础 4.2.1 配置数据库连接池 4.2.2 数据库连接池的应用 4.2 -3实例:用doGet方法处理客户端请求 ...
4.1.1 实例:在My Eclipse中编写Helloworld程序 4.1.2 实例:手工编写:Helloworld程序 4.2 Servlet基础 4.2.1 配置数据库连接池 4.2.2 数据库连接池的应用 4.2 -3实例:用doGet方法处理客户端请求 ...
10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...
“Hello World”来快速搭建和配置第一个程序。然后通过4章来详细讲解ExtJS的基本功能,包括: 最常用的表单、面板和布局类,常用的工具类与函数,以及ExtJS对事件的响应。本书还结合现 今流行的Web框架进行改造,将...
考虑到ExtJS与主流服务端框架的整合越来越紧密,《ExtJS Web应用程序开发指南(第2版)》还特意讲解了Struts和Struts 2框架的整合与配置。最后全面细致地讲解了ExtJS 4.0中新增的主题、图形和图表部分,真正实现完全...
接着通过经典的“hello world”来快速搭建和配置第一个程序。然后通过4章来详细讲解extjs的基本功能,包括:最常用的表单、面板和布局类,常用的工具类与函数,以及extjs对事件的响应。本书还结合现今流行的web框架...