posts - 24,  comments - 25,  trackbacks - 0

          Struts2.0屬于Web框架,MVC 2模型,其實和以前的Struts1.x沒有什么關系,新手可以不用去學。
          因為Struts2.0是Webwork2.2演變而來。

          第一步:創建Web工程

          要使用Struts2.0先要去下載包
          http://people.apache.org/builds/struts/2.0.10/struts-2.0.10-lib.zip

          打開struts-2.0.10-lib.zip\struts-2.0.10\lib
          把里面commons-logging-1.0.4.jar;freemarker-2.3.8.jar;ognl-2.6.11.jar;struts2-core-2.0.11.jar;xwork-2.0.4.jar五個包解壓出來,拷貝到你的Web工程的WebContent/WEB-INF/lib下

          第二步:配置web.xml文件

          <filter>
            
          <filter-name>struts</filter-name>
            
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
          </filter>
          <filter-mapping>
            
          <filter-name>struts</filter-name>
            
          <url-pattern>/*</url-pattern>
          </filter-mapping>

           

          以上配置添加到<web-app></web-app>里
          但是我們有時會用到中文,所以我們要重寫FilterDispatcher(過濾器)

          在src目錄下創建com.filter包,在包中建立NewFilter類,繼承FilterDispatcher ,代碼如下:

          package filter;

          import org.apache.struts2.dispatcher.FilterDispatcher;
          import java.io.IOException;
          import javax.servlet.FilterChain;
          import javax.servlet.FilterConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;

          public class NewFilter extends FilterDispatcher {

           
          private static String encoding = "GB2312";

           
          public void init(FilterConfig filterConfig) throws ServletException {
            
          super.init(filterConfig);
            String encodingParam 
          = filterConfig.getInitParameter("encoding");
            
          if (encodingParam != null && encodingParam.trim().length() != 0{
             encoding 
          = encodingParam;
            }

           }


           
          public void doFilter(ServletRequest request, ServletResponse response,
             FilterChain chain) 
          throws IOException, ServletException {
            request.setCharacterEncoding(encoding);
            
          super.doFilter(request, response, chain);
           }

          }


          但是這樣改寫后發現沒效果,其實是web.xml里的配置沒有調用而已,所以web.xml的配置要改成:
          <filter>
            
          <filter-name>struts2</filter-name>
            
          <filter-class>com.filter.NewFilter</filter-class>
            
          <init-param>
              
          <param-name>encoding</param-name>
              
          <param-value>GB2312</param-value>
            
          </init-param>
          </filter>
          <filter-mapping>
            
          <filter-name>struts2</filter-name>
            
          <url-pattern>/*</url-pattern>
          </filter-mapping>

          不難發現,NewFilter.java里的 private static String encoding = "GB2312";
          和web.xml里的<param-name>encoding</param-name><param-value>GB2312</param-value>
          其實String encodingParam = filterConfig.getInitParameter("encoding");
          就是從web.xml中讀出參數名為encoding的值,然后賦給子類中的encoding成員

          所以,以后需要改變編碼方式只需在web.xml中改 <param-value>的值

          第三步:配置struts.xml

          在src里新建一個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="com" extends="struts-default">    
                  
          <action name="hello" class="com.HelloWorld">
                    
          <result>Hello.jsp</result>
                  
          </action>
              
          </package>
          </struts>

          添加到struts.xml里

          在src下新建struts.properties添加如下配置:

          struts.locale=zh_CN
          struts.i18n.encoding=GB2312


          這樣struts就能識別中文了


          第四步:新建JavaBean

          在src.com下新建HelloWorld.java,代碼如下:
          package com;

          import com.opensymphony.xwork2.ActionSupport;

          public class HelloWorld extends ActionSupport {
           
          private static final long serialVersionUID = 1L;
           
           
          private String message;

           
          public String getMessage() {
            
          return message;
           }


           
          public void setMessage(String message) {
            
          this.message = message;
           }

           
           
          public String execute() {
            System.out.println(
          "Executing action, your message is " + message);
            
          return SUCCESS;
           }

          }



          第五步:新建jsp頁面

          在WebContent下新建HelloWorld.jsp,代碼如下:

          <%@ page contentType="text/html; charset=GB2312" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
              
          <head>
                  
          <title>HelloWorld</title>
                  
          <s:head />
              
          </head>
              
          <body>
                  
          <s:actionerror />
                  
          <s:form action="hello">
                      
          <s:textfield label="Name" name="message" tooltip="Enter your Name here" />
                      
          <s:submit />
                  
          </s:form>
              
          </body>
          </html>


          在WebContent下新建Hello.jsp,代碼如下:

          <%@ page contentType="text/html; charset=GB2312" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
              
          <head>
                  
          <title>HelloWorld</title>
                  
          <s:head />
              
          </head>
              
          <body>
                  
          <h1><s:property value="message" /></h1>
              
          </body>
          </html>


          以上工作完成可以把web工程打包,發布到tomcat或其他web服務器
          在地址欄輸入:http://localhost:8080/你的web工程名稱/HelloWorld.jsp

          在輸入框輸入信息點擊submit提交,會在Hello.jsp上顯示出你剛才輸入的信息

          總結:

          上面的例子簡單地演示了,Web 應用程序的基本操作。第一,配置并不復雜;第二,action提交方式的改變,直接在struts.xml里配置,而不再像1.x里使用

          request.forwardmapping("");

          來提交;第三,struts2的標簽庫用起來更簡單。

          posted on 2008-01-10 12:02 Jarry 閱讀(1183) 評論(0)  編輯  收藏 所屬分類: Struts2.x
          主站蜘蛛池模板: 新和县| 印江| 廉江市| 交城县| 洛川县| 鄢陵县| 海门市| 平罗县| 九江市| 新津县| 锡林郭勒盟| 远安县| 搜索| 金平| 运城市| 固镇县| 楚雄市| 鄂托克旗| 永嘉县| 吴堡县| 治多县| 德阳市| 泸溪县| 固原市| 潞西市| 平阴县| 汝南县| 灵璧县| 宿松县| 商南县| 通化县| 三原县| 大竹县| 永定县| 乌鲁木齐市| 北海市| 平江县| 民勤县| 乌审旗| 蒙阴县| 南投市|