使用Eclipse配置Struts2开发环境

来源:互联网 发布:编程让系统死机 编辑:程序博客网 时间:2024/06/05 15:45
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 plaincopy
  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接口:



按如下内容编辑该类:

  • package com.mystruts.action;  
  •   
  • import com.opensymphony.xwork2.Action;  
  •   
  • /**  
  •  * @author david  
  •  *  
  •  */  
  • public class HelloWorld implements Action {  
  •   
  •     private String message;  
  •       
  •       
  •     /**  
  •      * @return the message  
  •      */  
  •     public String getMessage() {  
  •         return message;  
  •     }  
  •   
  •   
  •     /* (non-Javadoc)  
  •      * @see com.opensymphony.xwork2.Action#execute()  
  •      */  
  •     @Override  
  •     public String execute() throws Exception {  
  •         // TODO Auto-generated method stub  
  •         message = "Hello World!";  
  •         return SUCCESS;  
  •     }  
  •   
  • }  

  • 5、添加jsp页面

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

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


    JSP Files的编码选择UTF-8



    然后添加HellowWorld.jsp文件:



    按下文编辑HelloWorld.jsp文件:

    [html] view plaincopy
    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配置文件:


    文件内容如下:

    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/struts/Hello



  • 0 0
    原创粉丝点击