使用Eclipse配置Struts2开发环境

Elipcse版本:Luna Service Release 1 (4.4.1),下载地址:https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR1/eclipse-jee-luna-SR1-win32.zip

Apache版本:apache-tomcat-6.0.37

Struts2:本次使用版本2.3.16.3.GA,下载地址http://struts.apache.org/download.cgi#struts23163



如上图,选择下载struts-2.3.16.3-all.zip并解压


Eclipse和Tomcat的安装不再细说了,注意看一下Struts2项目的配置:

1、创建动态web项目

在Eclipse中选择File-New-Dynamic Web Project创建动态项目:


按下图创建名称为Struts2HelooWorld的项目,在Target runtime中选择Apache Tomcat v6.0并按提示选择Tomcat的安装根目录,单击“Finish”完成项目创建



2、添加Struts2 库到项目

在解压的struts-2.3.16.3-all目录下的lib目录中复制需要的库,并在Struts2HelloWorld项目的WebContent/WEB-INF/lib上右键粘贴:



需要导入的库如下,每个库的说明请参加struts2的文档:


3、配置过滤器

在Struts2HelloWorld项目的WebContent/WEB-INF/web.xml中添加如下配置:


如下:

[html]  view plain copy
  1. <filter>    
  2.         <filter-name>struts2</filter-name>    
  3.         <filter-class>    
  4.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter    
  5.         </filter-class>    
  6.     </filter>    
  7.     <filter-mapping>    
  8.         <filter-name>struts2</filter-name>    
  9.         <url-pattern>/*</url-pattern>    
  10.     </filter-mapping>  


4、创建action

按下图创建一个包:



在该包中增加一个HelloWorld类,实现xwork2的Action接口:



按如下内容编辑该类:

[html]  view plain copy
  1. package com.mystruts.action;  
  2.   
  3. import com.opensymphony.xwork2.Action;  
  4.   
  5. /**  
  6.  * @author david  
  7.  *  
  8.  */  
  9. public class HelloWorld implements Action {  
  10.   
  11.     private String message;  
  12.       
  13.       
  14.     /**  
  15.      * @return the message  
  16.      */  
  17.     public String getMessage() {  
  18.         return message;  
  19.     }  
  20.   
  21.   
  22.     /* (non-Javadoc)  
  23.      * @see com.opensymphony.xwork2.Action#execute()  
  24.      */  
  25.     @Override  
  26.     public String execute() throws Exception {  
  27.         // TODO Auto-generated method stub  
  28.         message = "Hello World!";  
  29.         return SUCCESS;  
  30.     }  
  31.   
  32. }  

5、添加jsp页面

在添加jsp文件之前,首先我们修改一下eclipse的默认项目编码,否则中文会是乱码,修改方法如下:

选择eclipse的主菜单中的window-Preferences


JSP Files的编码选择UTF-8



然后添加HellowWorld.jsp文件:



按下文编辑HelloWorld.jsp文件:

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <%@ taglib prefix="s" uri="/struts-tags" %>  
  5.   
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>欢迎界面</title>  
  10. </head>  
  11. <body>  
  12. <h2><s:property value="message"/></h2>  
  13. </body>  
  14. </html>  

6、添加struts.xml配置文件:


文件内容如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="HelloWorld" extends="struts-default">  
  8.         <action name="HelloWorld" class="com.mystruts.action.HelloWorld">  
  9.             <result name="success">/HelloWorld.jsp</result>  
  10.         </action>       
  11.     </package>  
  12. </struts>  

7、调试





在浏览器中输入:http://localhost:8080/Struts2HelloWorld/HelloWorld

结果如下:

猜你喜欢

转载自blog.csdn.net/axwolfer/article/details/40057941