使用Eclipse配置Struts2开发环境

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接口:



按如下内容编辑该类:

  • 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 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配置文件:


    文件内容如下:

    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值