Struts2 操练宝典 ( 入门 实战 笔记 教程 )

Eclipse:Juno

文件结构:

文件结构截图

注意上图中的 9 个 jar 包。全部可以在 struts-2.3.9-lib.zip 中找到。

所有 jar 包截图:

Struts 下载地址:http://people.apache.org/builds/struts/2.3.9/

WebContent/WEB-INF/web.xml 内容:

 1 <web-app id="WebApp_9" version="2.4"   
 2     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 
 5         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
 6     
 7 
 8   <display-name>struts2</display-name>
 9     
10     
11     <filter>
12         <filter-name>struts2</filter-name>
13         <filter-class>
14             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
15            </filter-class>
16         <init-param>
17             <param-name>actionPackages</param-name>
18             <param-value>com.mycompany.myapp.actions</param-value>
19         </init-param>
20     </filter>
21 
22     <filter-mapping>
23         <filter-name>struts2</filter-name>
24         <url-pattern>/*</url-pattern>
25     </filter-mapping>
26   
27   
28   
29   
30   <welcome-file-list>
31     <welcome-file>index.html</welcome-file>
32     <welcome-file>index.htm</welcome-file>
33     <welcome-file>index.jsp</welcome-file>
34     <welcome-file>default.html</welcome-file>
35     <welcome-file>default.htm</welcome-file>
36     <welcome-file>default.jsp</welcome-file>
37   </welcome-file-list>
38 
39 
40 </web-app>

Apache 官方推荐的 web.xml 请参考 Struts 文档 http://struts.apache.org/development/2.x/docs/webxml.html

src/struts.xml 内容:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
 4 
 5 <struts>
 6 
 7     <include file="struts-default.xml"></include>
 8 
 9    
10 
11     <package name="struts2" extends="struts-default">
12 
13        <action name="helloWorld" class="helloWorld.Action">
14 
15            <result>/helloWorld/main.jsp</result>
16 
17        </action>
18 
19     </package>
20 
21 </struts>

src/helloWorld/Action.java 内容:

 1 package helloWorld;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 @SuppressWarnings({ "serial" })
 6 public class Action extends ActionSupport {
 7     
 8      @Override
 9 
10         public String execute() throws Exception {
11 
12            System.out.println("Action 执行了。");
13 
14            return SUCCESS;
15 
16         }
17      
18      public Action(){
19          System.out.println("Action 的确是执行了。");
20      }
21 
22 }

WebContent/helloWorld/main.jsp 内容:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <%
 4 
 5     String path = request.getContextPath();
 6 
 7     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 8 
 9 %>
10 
11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
12 
13 <html>
14 
15  <head>
16 
17     <base href="<%=basePath%>">
18 
19    
20 
21     <title>My JSP 'result.jsp' starting page</title>
22 
23    
24 
25     <meta http-equiv="pragma" content="no-cache">
26 
27     <meta http-equiv="cache-control" content="no-cache">
28 
29     <meta http-equiv="expires" content="0">   
30 
31     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
32 
33     <meta http-equiv="description" content="This is my page">
34 
35     <!--
36 
37     <link rel="stylesheet" type="text/css" href="styles.css">
38 
39     -->
40 
41  </head>
42 
43  
44 
45  <body>
46 
47     世界,你好. <br>
48 
49  </body>
50 
51 </html>
52 
53 
54 <!-- 
55 
56     http://localhost:8080/struts2/helloWorld.action
57 
58  -->

执行结果:

Eclipse 控制台输出:

参考资料:http://www.blogjava.net/lzhidj/archive/2008/07/08/213445.html 

if 需要源码 then 留下邮箱。

原文地址:https://www.cnblogs.com/livon/p/2975198.html