ITeye博客

struts2 简单配置

xycdgc · 2010-04-14 17:16 · Java · 企业架构 · Markdown

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,在与之间添加的代码是:

 <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,在运行时是对应的。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。