web 08 struts2入門 struts2配置 struts包

電影網站:www.aikan66.com css

項目網站:www.aikan66.com 
遊戲網站:www.aikan66.com 
圖片網站:www.aikan66.com 
書籍網站:www.aikan66.com 
學習網站:www.aikan66.com 
Java網站:www.aikan66.com 
iOS網站:www.aikan66.comhtml

----java

一、下載Struts2的jar包
下載地址:http://archive.apache.org/dist/struts/binaries/
我用的是struts-2.3.8-all.zip這個版本web

----apache

二、建立一個web project項目
下面給出全部文件均建立完成後的工程師圖。app

----jsp

三、導入Struts2所需jar包工具

第1步驟中下載的文件解壓,apps>>struts2-blank>>WEB-INF>>lib目錄中的文件學習

拷貝到項目的lib目錄下面網站

----

四、配置web.xml,在其中註冊過濾器
web.xml完整代碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>5</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>
    <!-- Struts2配置 -->
  <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>
</web-app>

注意:上面的代碼是Struts2.1以後的配置方法。若是你是Struts2.0須要更換一行代碼

org.apache.struts2.dispatcher.FilterDispatcher

----

五、寫action類(HelloWorld.java)

這個類主要用於struts2跳轉到這個action後。默認執行execute()方法。並根據結果返回字符,而後struts.xml根據返回的字符跳到相應的頁面。

package tutorial;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport
{
    public final static String MESSAGE = "Struts is up and running !";
    private String message;

    public String getMessage(){
        return message;
    }
    /**
     * @pram message the message to set
     */
    public void setMessage(String message){
        this.message=message;
    }
    
    public String execute() throws Exception
    {
        setMessage(MESSAGE);
        return SUCCESS;
    }
}

----

六、配置struts.xml

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


    <struts>
        <package name="struts2" extends="struts-default">
            <action name="HelloWorld" class="tutorial.HelloWorld">
                <result>/HelloWorld.jsp</result>
            </action>
        </package>
    </struts>

其中,package元素,做用相似於Java包的機制,他是用於分門別類的一個工具,extends屬性如他的名字同樣,它繼承了struts-default這個包的全部信息,通常咱們本身建立一個包最好都繼承它,由於他爲咱們提供了絕大部分的功能,你能夠在struts2-core的jar包中的struts-default.xml文件中找到這個包。action元素對應與你的表單,例如你的表單的action="welcome",那麼該表單提交後就會將參數交予action的name="welcome"的實現類處理。result元素爲action的結果,它由動做類返回的控制字段選擇。

----

七、寫jsp頁面(HelloWorld.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'HelloWorld.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
   <h2><s:property value="message" /></h2>  
  </body>
</html>

 

----

八、部署運行

http://localhost:8080/Struts2Demo/HelloWorld

--

完畢

注意的是

相關文章
相關標籤/搜索