例子使用的struts版本是目前的最新版struts-2.1.8.1
1. 到官方网站下载struts2.X最新版本http://struts.apache.org/2.x/index.html
2. 参考struts2-blank-2.1.8.1项目,用MyEclipse新建S2-001-HelloWorld项目
a) 解压apps下的struts2-blank-2.1.8.1.war包作为自己项目的参考
b) 需要的jar包
i. 将struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\lib下的所有jar包拷贝到 S2-001-HelloWorld项目WEB-INF\lib下
c) struts2配置文件
i. 参考struts2-blank-2.1.8.1\WEB-INF\classes下的struts.xml文件。修改后如下:
d) web.xml文件
i. 参考struts2-blank-2.1.8.1\WEB-INF下的web.xml文件,修改后如下:
e) hello.jsp页面
3. 部署测试
a) 在浏览器地址栏输入http://localhost:8080/S2-001-HelloWorld/hello.action
4. 小结
a) 执行过程
i.当在地址栏输入http://localhost:8080/S2-001-HelloWorld/hello.action后
ii.web服务器(Tomcat等)根据URL会读取S2-001-HelloWorld项目WEB-INF下的的web.xml文件。
iii.根据配置将请求转交给配置好的struts2过滤器:StrutsPrepareAndExecuteFilter处理,执行doFilter等方法。
iv.StrutsPrepareAndExecuteFilter会读取struts.xml配置文件,由配置中指定的action处理请求并指派result配置的视图显示给用户
5. 补充
a) 配置MyEclipse可直接查看Struts2源代码
b) 将Struts2的API文档关联到MyEclipse中
c) 在struts.xml文件中配置成可自动提示struts标签
i. window –-> preferences –-> 搜索 catalog –-> add
ii.key type为URI类型
iii.key: http://struts.apache.org/dtds/struts-2.0.dtd
iv.location: 对应的dtd文件,位于struts-core包中,解压开,指定相应位置,如:***\struts-2.1.8.1\lib\struts2-core-2.1.8.1\struts-2.0.dtd
1. 到官方网站下载struts2.X最新版本http://struts.apache.org/2.x/index.html
2. 参考struts2-blank-2.1.8.1项目,用MyEclipse新建S2-001-HelloWorld项目
a) 解压apps下的struts2-blank-2.1.8.1.war包作为自己项目的参考
b) 需要的jar包
i. 将struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\lib下的所有jar包拷贝到 S2-001-HelloWorld项目WEB-INF\lib下
c) struts2配置文件
i. 参考struts2-blank-2.1.8.1\WEB-INF\classes下的struts.xml文件。修改后如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
hello.jsp
</result>
</action>
</package>
</struts>
d) web.xml文件
i. 参考struts2-blank-2.1.8.1\WEB-INF下的web.xml文件,修改后如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
e) hello.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Hello Struts2. <br>
</body>
</html>
3. 部署测试
a) 在浏览器地址栏输入http://localhost:8080/S2-001-HelloWorld/hello.action
4. 小结
a) 执行过程
i.当在地址栏输入http://localhost:8080/S2-001-HelloWorld/hello.action后
ii.web服务器(Tomcat等)根据URL会读取S2-001-HelloWorld项目WEB-INF下的的web.xml文件。
iii.根据配置将请求转交给配置好的struts2过滤器:StrutsPrepareAndExecuteFilter处理,执行doFilter等方法。
iv.StrutsPrepareAndExecuteFilter会读取struts.xml配置文件,由配置中指定的action处理请求并指派result配置的视图显示给用户
5. 补充
a) 配置MyEclipse可直接查看Struts2源代码
b) 将Struts2的API文档关联到MyEclipse中
c) 在struts.xml文件中配置成可自动提示struts标签
i. window –-> preferences –-> 搜索 catalog –-> add
ii.key type为URI类型
iii.key: http://struts.apache.org/dtds/struts-2.0.dtd
iv.location: 对应的dtd文件,位于struts-core包中,解压开,指定相应位置,如:***\struts-2.1.8.1\lib\struts2-core-2.1.8.1\struts-2.0.dtd