论坛首页 Java企业应用论坛

(二)实例理解struts2之action

浏览 1834 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-22   最后修改:2009-09-22

-------------------web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
    <display-name>
    Struts2_HelloWorld</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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.html</welcome-file>
    </welcome-file-list>
</web-app>

 

 

 

---------------struts.xml

 

 

<!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 ="ActionDemo" extends ="struts-default">
    <action name ="HelloWorld " class ="tutorial.HelloWorld">
        <result>/HelloWorld.jsp</result>
    </action>
    <action name="AliasHelloWorld " class="tutorial.HelloWorld" method="aliasAction ">
        <result>/HelloWorld.jsp</result>
        </action>
        <action name="TestHelloWorld " class="tutorial.HelloWorld" method="testAction ">
        <result>/HelloWorld.jsp</result>
        </action>
    </package>
 </struts>

 

 

 

-------------------------HelloWorld.java

 

 

package tutorial;
 import java.text.DateFormat;
import java.util.Date;
 import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorld extends ActionSupport  {
    private String message;
  
    public String getMessage() {
        return message;
   }
  
   @Override
    public String execute () {
       message = " Hello World, Now is " + DateFormat.getInstance().format( new Date());
        return SUCCESS;
   }
   public String aliasAction () {
    message ="自定义Action调用方法";
    return SUCCESS;
  } 
     public String testAction () {
    message ="test:自定义Action调用方法";
    return SUCCESS;
    }
}

 

 

浏览器地址栏中键入http://localhost:8080/Struts2_Action/AliasHelloWorld.action 或 http://localhost:8080/Struts2_Action/HelloWorld!aliasAction.action

 

 

 

 

 

 

压缩包里都有源码和发布包,都是我边学边做的测试,没有什么高深的代码,主要是了解struts2的基本用法,结构和思想。

 

下面有三个步骤,都是显示三个简单的演示,有些注释也会在里面。

 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics