Struts2是Apache下的一个开源在view层中很实用的框架。

(1)apache的网站,到download项下载struts。

(2)用myeclipse创建一个web项目,叫做first_struts,然后将struts2中的相应的freemarker,ognl,struts2-core,以及xwork四个jar文件放入到本项目web-INF下的lib中(四个jar文件,只列出名字了,在不同版本的struts2中寻找对应的)。然后刷新项目,类包就导入成功了。

(3)配置web-inf下的web.xml,在<web-app>与</web-app>之间添加的代码是:

   
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
                org.apache.struts2.dispatcher.FilterDispacher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  

(4)配置struts.xml

   
<?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE struts PUBLIC "-//Apache Software  Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts>
    <include file="struts-default.xml"/>
       <package name="default" extends="struts-default">
         <action name="hello" class="com.controller.HelloAction">
            <result name="success">/success.jsp</result>
         </action>
       </package>
</struts>

(5)相应类的创建,在com.controller包中创建HelloAction类

   
package com.controller;

public class HelloAction {
	public String execute(){
		return "success";
	}

}
 

(6)创建相应的jsp文件,名为success.jsp

  
<%@ page contentType="text/html;charset=GB2312"%>
<%
  out.println("一个最简单的struts2例子");
%>

(7)部署项目,然后开启tomcat,在地址栏中输入http://localhost:8080/first_struts/hello.action

(8)需要注意的就是在配置struts.xml中的action的name,在运行时是对应的。