计应134-李亚晴
这里使用的是MyEclipse集成开发环境。
(1)安装MyEclipse 8.5
(2)安装配置Tomcat6.0
- 下载apache-tomcat-6.0.18
- 启动MyEclipse 8.5,单击Window->Preferences命令,选择MyEclipse->servers->Tomcat->Tomcat6.x选项,如下图所示:
单击Tomcat home directory后面的Browse按钮,找到自己下载的Tomcat文件夹,设置完成后,单击OK按钮。
(3)创建一个简单的 Struts 2程序
- 创建一个Web项目
- 为项目添加Struts jar包
3.创建Struts .xml配置文件
- 对默认的web.xml文件进行修改后的代码,web.xml是必需的,其他配置文件都是可选的,它是核心配置文件,用来对整个项目进行部署配置。对于Struts 2框架而言,如果要加载控制器FilterDispatcher,需要在web.xml文件中配置FilterDispatcher
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- 配置Struts.xml文件,该文件是Struts应用中的一个非常重要的核心配置文件,Struts 2框架根据struts.xml配置文件控制什么时候处理哪个程序以及如何进行处理
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="Struts2_OGNL_DEMO" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="helloworld" class="com.HelloWorldInterceptor"/> </interceptors> <action name="test" class="com.MyAction"> <result>success.jsp</result> <!-- action中引用拦截器 --> <interceptor-ref name="helloworld"/> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
6.创建Action业务控制器
7.创建JSP视图页面
- 总结:
- 在web.xml中需要加入Struts 2的加载配置
- 利用Action的execute()方法返回的是一个String
- 对默认的web.xml文件进行修改后的代码,web.xml是必需的,其他配置文件都是可选的,它是核心配置文件,用来对整个项目进行部署配置。对于Struts 2框架而言,如果要加载控制器FilterDispatcher,需要在web.xml文件中配置FilterDispatcher