struts2框架开发的第一个应用

写这篇博文,主要是帮助那些刚接触struts2框架开发而不知所措的人,希望批评指正

一、先建立一个web project,命名为struts2

二、在webroot/WEB-INF/lib目录下添加如下jar文件

 

三、在src目录下建立一个test.struts2包,并在此包下面建立HelloWorldAction.java文件

 1 package test.struts2;
 2 
 3 public class HelloWorldAction {
 4     private String msg;
 5     
 6     public String getMessage() {
 7         return msg;
 8     }
 9 
10     public String execute(){
11         msg = "我的第一个struts2应用";
12         return "success";
13     }
14 }

 

四、在src目录下建立struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <constant name="struts.configuration.xml.reload" value="true"/>
  <constant name="struts.action.extension" value="do,action,xxx,aai"/>
  <package name="itcast" namespace="/test" extends="struts-default">
        <action name="helloworld" class="test.struts2.HelloWorldAction" method="execute" >
            <result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
  </package> 
  
  
</struts>

 

五、在webroot/WEB-INF目录下建立page文件夹,并建立一个jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'hello.jsp' starting page</title>
  
  </head>
  
  <body>
    ${message}
  </body>
</html>

 六、web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 七、运行struts2项目

run as ----》MyEclipse server Application---->tomcat 6.0

八、在浏览器地址栏输入http://localhost:8080/struts2/test/helloworld.action即可访问(其中action可以改为xxx,do,aai)

posted @ 2014-07-05 23:50  幽林孤狼  阅读(1480)  评论(0编辑  收藏  举报