一:导入jar包
二:导入struts.xml
配置struts.xml
<struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hello"> <result> /hello.jsp </result> </action> </package> <!-- Add packages here --> </struts>
三:配置web.xml
(1)找web.xml
(2)关联源代码(JAVA):
struts.jar-->properties-->Java Resources Attach-->External location-->
(3)修改内容
<?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"
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 6 <display-name>Struts Blank</display-name> 7 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 </filter> 12 13 <filter-mapping> 14 <filter-name>struts2</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping> 17 18 </web-app>
四:新建hello.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 helloStruts2!!! 11 </body> 12 </html>
五:执行结果
(1):直接运行项目出错,没有启动hello.jsp
(2):地址栏输入hello.jsp 没有使用struts ,而使用的Model1
(3):地址栏输入hello或hello.action 使用struts
(4):修改action的name后,可以重启tomcat;清理缓存;eclipse;加代码(一直处于开发模式)
1 <constant name="struts.devMode" value="true" />