Struts2_HelloWorld_3

struts.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     <constant name="struts.configuration.xml.reload" value="true"/>
 8     <!-- 
 9     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
10     <constant name="struts.devMode" value="true" />
11 
12     <package name="default" namespace="/" extends="struts-default">
13 
14         <default-action-ref name="index" />
15 
16         <global-results>
17             <result name="error">/error.jsp</result>
18         </global-results>
19 
20         <global-exception-mappings>
21             <exception-mapping exception="java.lang.Exception" result="error"/>
22         </global-exception-mappings>
23 
24         <action name="index">
25             <result type="redirectAction">
26                 <param name="actionName">HelloWorld</param>
27                 <param name="namespace">/example</param>
28             </result>
29         </action>
30     </package>
31     <include file="example.xml"/>
32      -->
33 
34     <!-- Add packages here -->
35     
36     <package name="default" namespace="/" extends="struts-default">
37         <action name="hello">
38             <result>/Hello.jsp</result>
39         </action>
40     </package>
41     
42 </struts>
View Code

namespace 为 "/",action 的 name 为 hello,访问路径就是:http://localhost:8080/Struts2_0100_Introduction/hello.action (.action 可以不要)

Hello.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <base href="<%=basePath%>" />
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Hello Struts2</title>
13 </head>
14 <body>
15 <h1>Hello Struts2</h1>
16 </body>
17 </html>
View Code

部署后即可访问。 

代码链接:http://pan.baidu.com/s/1miAy27I 提取码:fhhk

原文地址:https://www.cnblogs.com/ShawnYang/p/6669108.html