---
title: "struts2 简单配置"
author: "xycdgc"
date: "2010-04-14T17:16:49Z"
tags: []
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2010-04-27T15:53:50Z"
id: 643902
url: "https://xycdgc.iteye.com/blog/643902"
---

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>之间添加的代码是：
    
```xml
 <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
<?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类
    
    
```java
 package com.controller;

public class HelloAction {
	public String execute(){
		return "success";
	}

}
```
  
（6）创建相应的jsp文件，名为success.jsp

   
```jsp
<%@ page contentType="text/html;charset=GB2312"%>
<%
  out.println("一个最简单的struts2例子");
%>
```

（7）部署项目，然后开启tomcat,在地址栏中输入http://localhost:8080/first_struts/hello.action
（8）需要注意的就是在配置struts.xml中的action的name，在运行时是对应的。