1.下载myeclipse
2.file–new–other–Web Project
3.配置tomcat路径 ,使用此配置的路径对应的tomcat来发布web程序
- Windows–preferences–myeclipse–Servers–Tomcat
- 将其改为enable
4.配置运行时的jdk
- Windows–preferences–java–Installed JREs
5.修改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>
<!-- devMode为开发者模式true表示开启可以不用重启tomcat也使配置stuts.xml的修改生效-->
<constant name="struts.devMode" value="true" />
<!--namespace为命名空间,此处值为"/","/"代表url中"http://localhost/Struts2_0100_Introduction/"部分,其内部的/Hello.jsp标志表示http://localhost/Struts2_0100_Introduction/Hello.jsp-->
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
</struts>
6.将WebRoot下的*.jsp改为Hello.jsp
7.修改WEB-INF/web.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<!-- "/*"永远这么写,"/"也是代表"http://localhost/Struts2_0100_Introduction/","/*"的意思就是无论"http://localhost/Struts2_0100_Introduction/"后面接的是什么路径,都走到这个名为struts2的filter中,也可以写*.action,*.action表示所有的action-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8.复制WEB-INF/lib下的所有jar包到自己项目下的WEB-INF的lib文件夹中
9.在对应tomcat上部署服务
- 右键项目–Debug As–MyEclipse Server Application–刚自己配置的tomcat
10.在浏览器中打开http://localhost:8080/Struts2_0100_Introduction/hello
11.报找不到目录错误
- 检查上面配置没有问题 应该是struts的jar包和tomcat版本不符的问题 现我使用的是struts2.3 tomcat8
12.使jar包中.class关联源码 即可以正常看到类中的内容而不是编译后的内容
右键项目–properties–Java Build Path–Libraries–点开对应jar包–双击Source attachment–External Folder–struts目录/src/core/src/main/java,此时可以打开jar包中的类看到源码
右键Web App Liberaries下的jar包–properties–Java Source Attachment–External Folder–struts2目录/src/core/src/main/java
13.看源码时同时看到javadoc文档
- 右键Web App Liberaries下的jar包–properties–JavaDoc Location–Javadoc URL–Browse–sturts2目录/soft/struts2.3.1/docs/struts2-core/apidocs,此时在使用该jar包中的类时按F1–javadoc forXXX
14.在xml中敲”<” 没有关键字提示
- 在xml最上查看定义该xml文件语法的文件的位置为http://struts.apache.org/dtds/struts-2.0.dtd
- Window–Preferences–XML Catalog(Catalog)–add
- key:http://stuts.apache.org/dtds/stuts-2.0.dtd
- key type:URI
- Location:随便找个stuts2的jar包解压开里面有.Dtd文件
15.Struts运行机制
- url–找到指定webapplication(Struts2_0100_Introduction)–读取其web.xml–匹配url- pattern–找到对应filter类–这个类会查找struts.xml中匹配的namespace–找到匹配的package,找到其内部的action–调用action对应java类的execute方法找到result名–找到其内对应的result,返回result中的内容
16.sturts2的好处
- 提供可扩展性(方便修改),灵活性,将请求与视图分离(作用)