配置文件主要有
Struts.xml
web.xml
其中Stuts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.devMode" value="true" /><!--true表示现在为开发模式 --> 9 <!-- 即修改配置不需要重启服务器 --> 10 <package name="default" namespace="/" extends="struts-default"> 11 <action name="hello"> 12 <!-- result 即Action处理后返回给用户的师徒资源,下面配置了一个result --> 13 <!-- result不填写任何属性时,默认触发条件为SUCCESS --> 14 <result>/Hello.jsp</result> 15 </action> 16 </package> 17 18 19 </struts>
Web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name></display-name> 8 9 <!-- 加载Struts 2的核心控制器 用来对事件作出处理 --> 10 <filter> 11 <filter-name>struts2</filter-name> 12 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 13 </filter> 14 <!-- 使用通配符/*来拦截所有的URL请求,保证用户请求都被Struts2截获 --> 15 <filter-mapping> 16 <filter-name>struts2</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 20 </web-app>
Hello.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>Struts2</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 Struts2. <br> 25 </body> 26 </html>
当向浏览器输入地址时,根据域名进入package搜索对应action.而后执行result中相应的result