2014年10月8日 星期三

Struts2

Struts2

jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.16.3.jar
xwork-core-2.3.16.3.jar

將這些jar檔放置在 WEB-INF底下lib資料夾當中



※配置WEB-INF底下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_2_5.xsd"
    version="2.5">

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

index.jsp
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=BIG5">
       <title>Insert title here</title>
      
       <style>
           body {background-color:lightgray}
           h1   {color:red}
       </style>
    </head>
    <body>
       <h1>Redirect . . .</h1>
    </body>
</html>


※在src目錄底下建立 struts.xml
struts.xml
xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
   
    <struts>
        <package name="default" extends="struts-default">
            <action name="index" class="nerdy.action.indexAction" method="index">
                <result name="success">/index/action.jsp</result>
            </action>       
        </package>
    </struts>

說明:
以上package nameaction name的部份可以自己命名
class=" nerdy.action.indexAction " 的部份需要對應到src底下package nerdy.actionindexAction.java
method="index" 則是java檔裡的函式名稱

IndexAction.java
package nerdy.action;

public class indexAction {
    public String index() {
       return "success";
    }
}

return "success"; 是為了要與 struts.xml 中的  /index/ action.jsp success


※在webContent底下建立一個叫index的資料夾,並在資料夾中建立一個action.jsp
action.jsp
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Insert title here</title>
</head>
    <body>
       My Struts2 World!!
    </body>
</html>

將專案放到tomcat
然後瀏覽器網址列
打上http://localhost:8080/xxxxxx/
xxxxxxx為專案名稱
應出現index.jsp劃面

打上http://localhost:8080/xxxxxx/index.action
應出現action.jsp劃面



1 則留言 :