eclipse 配置struts2

1、創建工程項目:
        File->new->others
,打開新建嚮導對話框,在樹中找到web->Dynamic Web Project,選中,點擊next按鈕。在projects name中輸入dodotest,點擊next。保持默認設置,點擊finished。這時,我們在eclipse中會看到新建的工程dodotest,創建完成。


2
、修改web.xml文件:

      
內容如下

Xml代碼

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.     <display-name>  
  4.     dodotest</display-name>  
  5.       
  6.     <filter>  
  7.         <filter-name>struts2</filter-name>  
  8.         <filter-class>  
  9.             org.apache.struts2.dispatcher.FilterDispatcher   
  10.         </filter-class>  
  11.     </filter>  
  12.   
  13.     <filter-mapping>  
  14.         <filter-name>struts2</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  
  17.       
  18.     <welcome-file-list>  
  19.         <welcome-file>index.jsp</welcome-file>  
  20.     </welcome-file-list>  
  21. </web-app>  

<?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>

    dodotest</display-name>

  

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.FilterDispatcher

        </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>



3
、編寫類代碼:
3.1 HelloWorld.java
先寫一個父類

Java代碼

  1. package com.yeepal.test;   
  2.   
  3. public class HelloWorld {   
  4.     private String words;   
  5.   
  6.     public String getWords() {   
  7.         return words;   
  8.     }   
  9.   
  10.     public void setWords(String words) {   
  11.         this.words = words;   
  12.     }   
  13. }  

package com.yeepal.test;

 

public class HelloWorld {

    private String words;

 

    public String getWords() {

        return words;

    }

 

    public void setWords(String words) {

        this.words = words;

    }

}



3.2 HelloAction.java
再寫一個子類

Java代碼

  1. package com.yeepal.test;   
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import com.yeepal.test.HelloWorld;   
  5.   
  6. public class HelloAction extends ActionSupport {   
  7.     private static final long serialVersionUID = 1L;   
  8.   
  9.     private HelloWorld helloWorld;   
  10.   
  11.     public HelloWorld getHelloWorld() {   
  12.         return helloWorld;   
  13.     }   
  14.   
  15.     public void setHelloWorld(HelloWorld helloWorld) {   
  16.         this.helloWorld = helloWorld;   
  17.     }   
  18.   
  19.     @Override  
  20.     public String execute() {   
  21.         return SUCCESS;   
  22.     }   
  23. }  

package com.yeepal.test;

 

import com.opensymphony.xwork2.ActionSupport;

import com.yeepal.test.HelloWorld;

 

public class HelloAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

 

    private HelloWorld helloWorld;

 

    public HelloWorld getHelloWorld() {

        return helloWorld;

    }

 

    public void setHelloWorld(HelloWorld helloWorld) {

        this.helloWorld = helloWorld;

    }

 

    @Override

    public String execute() {

        return SUCCESS;

    }

}



注:這裏分成兩個類來寫,是爲了順便實踐一下類的繼承,而不是必須的,只寫一個類也是可以的。

4
、加載相應的包:

       
寫完上面的類,會發現有些錯誤,主要是因爲我們這裏使用了STRUTS及一些相應的lib文件,而這些文件我們還沒添加到項目裏,所以下一步我們就來添加這些lib,本次要使用到的lib文件包括4個,分別是:struts2-core-2.0.9.jarfreemarker-2.3.8.jar ognl-2.6.11.jarxwork-2.0.4.jar,千萬不要一下子就把所有的struts裏面的lib文件全部拿過來用,反而會出錯。

       
把這寫文件放到 dodotest\WebContent\WEB-INF\lib 目錄下,在eclipse中右擊項目名稱,選擇properties,在新的屬性對話框裏,選擇java Build Path,然後再右側的對話框裏選擇Libraries標籤,然後是右邊的“Add External JARs…,找到剛纔那4lib文件,全選,再點打開,這樣,這4lib文件就被添加到了項目中,這時候你會發現剛纔的那些錯誤消失了。


5
、現在我們來寫struts.xml文件:

src目錄下新建XML文件,命名爲struts.xml,內容如下:

Xml代碼

  1. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  2.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  3. <struts>  
  4.     <include file="struts-default.xml" />  
  5.   
  6.     <package name="default" extends="struts-default">  
  7.         <action name="hello"  
  8.             class="com.yeepal.test.HelloAction">  
  9.             <result name="success">success.jsp</result>  
  10.             <result name="input">index.jsp</result>  
  11.         </action>  
  12.   
  13.     </package>  
  14. </struts>  

<!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.yeepal.test.HelloAction">

            <result name="success">success.jsp</result>

            <result name="input">index.jsp</result>

        </action>

 

    </package>

</struts>



6
、編寫JSP文件:
下面我們來寫兩個JSP文件,新建JSP文件,默認情況下新建的文件路徑是WebContent下,不需要修改。

Html代碼

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5. <head>  
  6. <title>你好,世界</title>  
  7. <meta http-equiv="pragma" content="no-cache">  
  8. <meta http-equiv="cache-control" content="no-cache">  
  9. <meta http-equiv="expires" content="0">  
  10. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11. <meta http-equiv="description" content="This is my page">  
  12. </head>  
  13. <body>  
  14. <form action="hello.action" method="post">  
  15. <fieldset><legend>Struts2入門實例</legend>  
  16. <p><label>請輸入你想說的話:</label>  
  17. <input type="text" name="helloWorld.words" value="試試看!" /></p>  
  18. <p><input type="submit" value="提交" /></p>  
  19. </fieldset>  
  20. </form>  
  21. </body>  
  22. </html>  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>你好,世界</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章