gdufo

           

          實戰(zhàn)Struts-Menu(4)

          /**
               * 從數(shù)據(jù)庫中讀取菜單配置信息
               * 
               * 
          @return
               
          */
              
          private Map[] getMenuComponents() {
                  ArrayList list 
          = new ArrayList();
                  Connection conn 
          = null;
                  PreparedStatement pstmt 
          = null;
                  ResultSet rest 
          = null;
                  String sql 
          = "select name,parent_name,title,location,description from menu_item order by id";
                  
          try {
                      Class.forName(
          "com.mysql.jdbc.Driver").newInstance();
                      conn 
          = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8");
                      pstmt 
          = conn.prepareStatement(sql);
                      rest 
          = pstmt.executeQuery();
                      
          while (rest.next()) {
                          
          int i = 1;
                          HashMap map 
          = new HashMap();
                          map.put(
          "name", rest.getString(i++));
                          map.put(
          "parent_name", rest.getString(i++));
                          map.put(
          "title", rest.getString(i++));
                          map.put(
          "location", rest.getString(i++));
                          map.put(
          "description", rest.getString(i++));
                          list.add(map);
                      }
                  } 
          catch (SQLException ex) {
                      ex.printStackTrace();
                  } 
          catch (InstantiationException e) {
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  } 
          finally {
                      
          try {
                          
          if (null!=rest) rest.close();
                          
          if (null!=pstmt) pstmt.close();
                          
          if (null!=conn) conn.close();
                      } 
          catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
                  
                  
          return (Map[]) list.toArray(new HashMap[0]);
              }
              
              
          /**
               * 構(gòu)造菜單權(quán)限
               * 
               * 
          @param request
               
          */
              
          private void buildMenuPermissions(HttpServletRequest request) {
                  PermissionsAdapter permession 
          = new PermissionsAdapter() {
                      
          public boolean isAllowed(MenuComponent menu) {
                          
          // 名稱等于StandaloneMenu的菜單不顯示
                          return !"StandaloneMenu".equalsIgnoreCase(menu.getName());
                      }
                  };
                  request.setAttribute(
          "examplesPermession", permession);
              }

              
          /**
               * 構(gòu)造菜單顯示標題
               * 
               * 
          @param request
               
          */
              
          private void buildMenuResourceBundle(HttpServletRequest request) {
                  MenuResourceBundle resourceBundle 
          = new MenuResourceBundle();
                  request.setAttribute(
          "examplesBundle", resourceBundle);
              }
              
              
          /**
               * MenuResourceBundle樹狀菜單國際語言顯示
               * 
               * 
          @author wenbin.zhang
               *  
               
          */
              
          class MenuResourceBundle extends ListResourceBundle {
                  
          private ArrayList list = new ArrayList();

                  
          public MenuResourceBundle() {
                      Connection conn 
          = null;
                      PreparedStatement pstmt 
          = null;
                      ResultSet rest 
          = null;
                      String sql 
          = "select title,titleCN from menu_item order by id";
                      
          try {
                          Class.forName(
          "com.mysql.jdbc.Driver").newInstance();
                          conn 
          = DriverManager.getConnection("jdbc:mysql://localhost/myexamples?user=root&password=mywangya&useUnicode=true&characterEncoding=UTF-8");
                          pstmt 
          = conn.prepareStatement(sql);
                          rest 
          = pstmt.executeQuery();
                          
          while (rest.next()) {
                              
          int i = 1;
                              String[] message 
          = new String[2];
                              message[
          0= rest.getString(i++);
                              
          try {
                                  message[
          1= new String(rest.getString(i++).getBytes("latin1"), "gbk");
                              } 
          catch (UnsupportedEncodingException e) {
                                  e.printStackTrace();
                              }
                              
          if (message[0!= null && message[1!= null) {
                                  list.add(message);
                              }
                          }
                      } 
          catch (SQLException ex) {
                          ex.printStackTrace();
                      } 
          catch (InstantiationException e) {
                          e.printStackTrace();
                      } 
          catch (IllegalAccessException e) {
                          e.printStackTrace();
                      } 
          catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      } 
          finally {
                          
          try {
                              
          if (null!=rest) rest.close();
                              
          if (null!=pstmt) pstmt.close();
                              
          if (null!=conn) conn.close();
                          } 
          catch (SQLException e) {
                              e.printStackTrace();
                          }
                      }

                  }

                  
          public Object[][] getContents() {
                      
          return (String[][]) list.toArray(new String[0][0]);
                  }
              }
          }

          > 將struts-config.xml文件的<action-mappings />部分修改為:
          <action-mappings>
            
          <action path="/menuAction" type="cn.appex.menu.MenuAction" >
              
          <forward name="success" path="/struts-menu/dynamic-menu.jsp" />
            
          </action>
          </action-mappings>

          posted @ 2008-08-05 15:05 gdufo| 編輯 收藏

          實戰(zhàn)Struts-Menu(3)

           插入測試數(shù)據(jù):
          INSERT INTO menu_item
              (id
          , parent_name, name, title, titleCN, location)
          VALUES
              (
          1,null,'DatabaseMenu','Database Menu','數(shù)據(jù)庫動態(tài)菜單',null),
              (
          2,'DatabaseMenu','Yahoo','Yahoo Mail','雅虎郵件','http://mail.yahoo.com'),
              (
          3,'DatabaseMenu','JavaBlogs','JavaBlogs','Java博客','http://javablogs.com'),
              (
          4,null,'StandaloneMenu','Standalone Menu','獨立的菜單','http://www.sohu.com')
          > 將數(shù)據(jù)庫驅(qū)動程序放到lib目錄中,并加入到Build Path,如MySQL的數(shù)據(jù)庫驅(qū)動mysql-connector-java-3.1.8-bin.jar,解壓縮commons- collections-3.1.zip,將commons-collections-3.1.jar釋放到lib目錄,并加入Build Path
            > 新建一個Struts的Action,代碼如下:

          package cn.appex.menu;

          import java.io.UnsupportedEncodingException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.ListResourceBundle;
          import java.util.Map;

          import com.mysql.jdbc.Driver;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;

          import net.sf.navigator.menu.MenuComponent;
          import net.sf.navigator.menu.MenuRepository;
          import net.sf.navigator.menu.PermissionsAdapter;

          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;

          public class MenuAction extends Action {
              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
                      
          throws Exception {
                  buildMenuRepository(request);
                  buildMenuResourceBundle(request);
                  buildMenuPermissions(request);
                  
          return (mapping.findForward("success"));
              }

              
          /**
               * 創(chuàng)建菜單數(shù)據(jù)結(jié)構(gòu)
               * 
               * 
          @param request
               
          */
              
          private void buildMenuRepository(HttpServletRequest request) {
                  MenuRepository repository 
          = new MenuRepository();
                  
          // Get the repository from the application scope - and copy the
                  
          // DisplayerMappings from it.
                  MenuRepository defaultRepository = (MenuRepository) request
                          .getSession().getServletContext().getAttribute(
                                  MenuRepository.MENU_REPOSITORY_KEY);
                  repository.setDisplayers(defaultRepository.getDisplayers());

                  Map[] menus 
          = getMenuComponents();
                  
          for (int i=0; i < menus.length; i++) {
                      MenuComponent mc 
          = new MenuComponent();
                      Map row 
          = menus[i];
                      String name 
          = (String) row.get("name");
                      mc.setName(name);
                      String parent 
          = (String) row.get("parent_name");
                      System.out.println(name 
          + ", parent is: " + parent);
                      
          if (parent != null) {
                          MenuComponent parentMenu 
          = repository.getMenu(parent);
                          
          if (parentMenu == null) {
                              System.out.println(
          "parentMenu '" + parent + "' doesn't exist!");
                              
          // create a temporary parentMenu
                              parentMenu = new MenuComponent();
                              parentMenu.setName(parent);
                              repository.addMenu(parentMenu);
                          }

                          mc.setParent(parentMenu);
                      }
                      String title 
          = (String) row.get("title");
                      mc.setTitle(title);
                      String location 
          = (String) row.get("location");
                      mc.setLocation(location);
                      String description 
          = (String) row.get("description");
                      mc.setDescription(description);
                      repository.addMenu(mc);
                  }
                  
                  request.setAttribute(
          "examplesRepository", repository);
              }

          posted @ 2008-08-05 15:02 gdufo| 編輯 收藏

          實戰(zhàn)Struts-Menu(2)

          4:解壓縮struts-menu-2.3.zip,將壓縮包中的jstl-1.0.6.jar、standard-1.0.6.jar、struts- menu-2.3.jar釋放到lib目錄中,將壓縮包中的struts-menu.tld、struts-menu-el.tld釋放到web\WEB -INF目錄中,解壓縮commons-lang-2.1.zip,將commons-lang-2.1.jar解壓縮到lib目錄中,注意,在 Struts-Menu的文檔中沒有看到需要這個包,但是沒有這個包卻無法成功加載。在web\WEB-INF目錄中新建menu- config.xml,內(nèi)容如下:
          <?xml version="1.0" encoding="UTF-8" ?>

          <MenuConfig>

            
          <Displayers>
              
          <Displayer   name="DropDown"
                           type
          ="net.sf.navigator.displayer.DropDownMenuDisplayer"/>
              
          <Displayer   name="Simple"
                           type
          ="net.sf.navigator.displayer.SimpleMenuDisplayer"/>
              
          <Displayer   name="CoolMenu"
                           type
          ="net.sf.navigator.displayer.CoolMenuDisplayer"/>
              
          <Displayer   name="CoolMenu4"
                           type
          ="net.sf.navigator.displayer.CoolMenuDisplayer4"/>
              
          <Displayer   name="MenuForm"
                           type
          ="net.sf.navigator.example.PermissionsFormMenuDisplayer"/>
              
          <Displayer   name="ListMenu"
                           type
          ="net.sf.navigator.displayer.ListMenuDisplayer"/>
              
          <Displayer   name="TabbedMenu"
                           type
          ="net.sf.navigator.displayer.TabbedMenuDisplayer"/>
              
          <Displayer   name="Velocity"
                           type
          ="net.sf.navigator.displayer.VelocityMenuDisplayer"/>
            
          </Displayers>

            
          <Menus>
              
          <Menu  name="DoorSite"  title="DoorSite"  description="Some famous doorsite" width="50">
                 
          <Item   name="Yahoo"   title="Yahoo">
                     
          <Item   name="YahooIndex"   title="Yahoo Index"   location="http://www.yahoo.com.cn"/>
                     
          <Item   name="YahooMail"    title="Yahoo Mail"    location="http://cn.mail.yahoo.com"/>
                 
          </Item>
                 
          <Item    name="Sohu"  title="Sohu"   location="http://www.sohu.com"/>
                 
          <Item    name="Sina"  title="Sina"   location="http://www.sina.com.cn"/>
              
          </Menu>
            
          </Menus>

          </MenuConfig>

          5:將第四步的示例程序中的struts-menu應用目錄下的images、scripts、styles三個目錄中的內(nèi)容復制到web目錄下

            6:你的目錄結(jié)構(gòu)應該類似如下:

          %PROJECT_HOME%\classes
              %PROJECT_HOME%\lib
              %PROJECT_HOME%\lib\antlr.jar
              %PROJECT_HOME%\lib\commons-beanutils.jar
              %PROJECT_HOME%\lib\commons-digester.jar
              %PROJECT_HOME%\lib\commons-fileupload.jar
              %PROJECT_HOME%\lib\commons-lang-
          2.1.jar
              %PROJECT_HOME%\lib\commons-logging.jar
              %PROJECT_HOME%\lib\commons-validator.jar
              %PROJECT_HOME%\lib\jakarta-oro.jar
              %PROJECT_HOME%\lib\jstl-
          1.0.6.jar
              %PROJECT_HOME%\lib\standard-
          1.0.6.jar
              %PROJECT_HOME%\lib\struts.jar
              %PROJECT_HOME%\lib\struts-menu-
          2.3.jar
              %PROJECT_HOME%\src
              %PROJECT_HOME%\src\log4j.properties
              %PROJECT_HOME%\src\application.properties
              %PROJECT_HOME%\src\application_zh_CN.properties
              %PROJECT_HOME%\web
           %PROJECT_HOME%\web\images\
           %PROJECT_HOME%\web\scripts\
           %PROJECT_HOME%\web\styles\
              %PROJECT_HOME%\web\WEB-INF
              %PROJECT_HOME%\web\WEB-INF\menu-config.xml
              %PROJECT_HOME%\web\WEB-INF\struts-bean.tld
              %PROJECT_HOME%\web\WEB-INF\struts-config.xml
              %PROJECT_HOME%\web\WEB-INF\struts-html.tld
              %PROJECT_HOME%\web\WEB-INF\struts-logic.tld
              %PROJECT_HOME%\web\WEB-INF\struts-menu.tld
              %PROJECT_HOME%\web\WEB-INF\struts-menu-el.tld
              %PROJECT_HOME%\web\WEB-INF\struts-nested.tld
              %PROJECT_HOME%\web\WEB-INF\struts-tiles.tld
              %PROJECT_HOME%\web\WEB-INF\web.xml

          六、實戰(zhàn)Struts-Menu
            1:使用配置文件實現(xiàn)靜態(tài)菜單
            新建JSP文件web/static-menu.jsp,內(nèi)容如下:

          <%@ page contentType="text/html; charset=GBK" %>
          <%@ taglib uri="struts-menu" prefix="menu" %>

          <menu:useMenuDisplayer name="ListMenu" 
              bundle
          ="org.apache.struts.action.MESSAGE">
              
          <menu:displayMenu name="DoorSite"/>
          </menu:useMenuDisplayer>

          運行Tomcat,在IE地址欄輸入http://localhost:8080/mymenu/static-menu.jsp查看

          2:實現(xiàn)中文化
            > 在src\application_zh_CN.properties中增加下面的內(nèi)容,Unicode可以通過JDK自帶的native2ascii工具得到:
          #門戶網(wǎng)站
          menu.DoorSite
          =\u95e8\u6237\u7f51\u7ad9
          #雅虎
          menu.Yahoo
          =\u96c5\u864e
          #雅虎首頁
          menu.YahooIndex
          =\u96c5\u864e\u9996\u9875
          #雅虎郵件
          menu.YahooMail
          =\u96c5\u864e\u90ae\u4ef6
          #搜狐
          menu.Sohu
          =\u641c\u72d0
          #新浪
          menu.Sina
          =\u65b0\u6d6a
          > 在src\application.properties中增加下面的內(nèi)容:

          #門戶網(wǎng)站
          menu.DoorSite
          =DoorSite
          #雅虎
          menu.Yahoo
          =Yahoo
          #雅虎首頁
          menu.YahooIndex
          =Yahoo Index
          #雅虎郵件
          menu.YahooMail
          =Yahoo Mail
          #搜狐
          menu.Sohu
          =Sohu
          #新浪
          menu.Sina
          =Sina
          > 修改menu-config.xml文件<Menus></Menus>部分:

          <Menus>
              
          <Menu  name="DoorSite"  title="menu.DoorSite"  description="Some famous doorsite" width="50">
                 
          <Item   name="Yahoo"   title="menu.Yahoo">
                     
          <Item   name="YahooIndex"   title="menu.YahooIndex"   location="http://www.yahoo.com.cn"/>
                     
          <Item   name="YahooMail"    title="menu.YahooMail"    location="http://cn.mail.yahoo.com"/>
                 
          </Item>
                 
          <Item    name="Sohu"  title="menu.Sohu"   location="http://www.sohu.com"/>
                 
          <Item    name="Sina"  title="menu.Sina"   location="http://www.sina.com.cn"/>
              
          </Menu>
            
          </Menus>
          重起Tomcat,在IE地址欄輸入http://localhost:8080/mymenu/static-menu.jsp查看

            3:結(jié)合數(shù)據(jù)庫實現(xiàn)動態(tài)菜單
            > 新建數(shù)據(jù)myexamples:
          CREATE DATABASE myexamples
            > 新建數(shù)據(jù)庫表menu_item:

          CREATE TABLE menu_item (
             id BIGINT not null
          ,
             parent_name VARCHAR(
          30),
             name VARCHAR(
          30),
             title VARCHAR(
          30),
             titleCN VARCHAR(
          30),
             description VARCHAR(
          50),
             location VARCHAR(
          255),
             target VARCHAR(
          10),
             onclick VARCHAR(
          100),
             onmouseover VARCHAR(
          100),
             onmouseout VARCHAR(
          100),
             image VARCHAR(
          50),
             altImage VARCHAR(
          30),
             tooltip VARCHAR(
          100),
             roles VARCHAR(
          100),
             page VARCHAR(
          255),
             width VARCHAR(
          5),
             height VARCHAR(
          5),
             forward VARCHAR(
          50),
             action VARCHAR(
          50),
             primary key (id)
          )

          posted @ 2008-08-05 14:56 gdufo| 編輯 收藏

          實戰(zhàn)Struts-Menu(1)

          2006-11-21 19:13
           一、簡介
            Struts-Menu是一組從基于XML的配置文件中生成多種樣式的菜單的JSP Tags,并且可以結(jié)合API開發(fā)通過數(shù)據(jù)庫生成的動態(tài)菜單。Struts-Menu支持國際化和多種權(quán)限控制。

          二、運行環(huán)境
            Windows 2000 Professional
            JDK 1.4.2_03
            Eclipse 3.1
            Tomcat 5.0.28
            Tomcat Plugin 3.1Beta
            Struts 1.2.7
            Commons-Lang 2.1
            Commons-Collections 3.1
            Struts-Menu 2.3
            MySQL 4.1.10a-nt

          三、下載與安裝
            1:從http://java.sun.com下載J2SDK,當前1.4.x系列的最新版本為1.4.2_08
            2:從http://www.eclipse.org下載Eclipse,當前最新版本為3.1正式版
            3:從http://jakarta.apache.org/tomcat下載Tomcat,當前5.x系列的最新版本為5.0.28
            4:從http://www.sysdeo.com/eclipse/tomcatplugin下載Eclipse的Tomcat插件,對應Eclipse3.1x的最新版本為3.1Beta
            5:從http://struts.apache.org下載Struts,當前最新版本為1.2.7
            6:從http://jakarta.apache.org/commons/下載Commons-Lang,當前最新版本為2.1,下載Commons-Collections,當前最新版本為3.1
            7:從http://struts-menu.sourceforge.net下載Struts Menu,當前最新版本為2.3
            8:從http://www.mysql.com下載MySQL數(shù)據(jù)庫,4.x系列的最新版本是4.1.12a
            9:MySQL、JDK、Eclipse、Tomcat和TomcatPlugin的安裝及配置請參考相關(guān)資料

          四、運行示例程序
            1:安裝好Tomcat后,解壓縮struts-menu-2.3.zip,將struts-menu.war釋放到Tomcat安裝目錄下的webapps下,運行Tomcat
            2:在地址欄輸入http://localhost:8080/struts-menu

          五、安裝與配置
            1:在Eclipse中新建Tomcat項目,Context為/mymenu,Subdirectory為/web
            2:在項目目錄下面新建lib目錄和web目錄及WEB-INF,在web/WEB-INF目錄下新建web.xml,內(nèi)容如下:


          <?xml version="1.0" encoding="ISO-8859-1"?>

          <!DOCTYPE web-app
              PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
              "http://java.sun.com/dtd/web-app_2_3.dtd"
          >

          <web-app>
            
          <display-name>My Example Application -- Vinton Lee</display-name>
                
            
          <!-- ============= The Struts ActionServlet Configuration ============= -->
            
          <servlet>
              
          <servlet-name>action</servlet-name>
              
          <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
              
          <init-param>
                
          <param-name>config</param-name>
                
          <param-value>/WEB-INF/struts-config.xml</param-value>
              
          </init-param>
              
          <load-on-startup>1</load-on-startup>
            
          </servlet>
            
          <!-- ================================================================== -->
              
            
          <!-- ============= The Struts Action Servlet Mapping ================== -->
            
          <servlet-mapping>
              
          <servlet-name>action</servlet-name>
              
          <url-pattern>*.do</url-pattern>
            
          </servlet-mapping>
            
          <!-- ================================================================== -->
            
            
          <!-- The Welcome File List -->
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>

            
          <!-- =============== The Struts Taglib Definition ===================== -->
            
          <taglib>
              
          <taglib-uri>struts-bean</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
            
          </taglib>
            
          <taglib>
              
          <taglib-uri>struts-html</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
            
          </taglib>
            
          <taglib>
              
          <taglib-uri>struts-logic</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
            
          </taglib>
            
          <taglib>
              
          <taglib-uri>struts-nested</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
            
          </taglib>
            
          <taglib>
              
          <taglib-uri>struts-tiles</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
            
          </taglib>
            
          <!-- ================================================================== -->
            
            
          <!-- ============= The Struts-Menu Taglib Definition ================== -->
            
          <taglib>
              
          <taglib-uri>struts-menu</taglib-uri>
              
          <taglib-location>/WEB-INF/struts-menu.tld</taglib-location>
            
          </taglib>
            
          <!-- ================================================================== -->
          </web-app>

          3:解壓縮struts-1.2.7.zip,將壓縮包中的lib目錄下所有的8個jar釋放到lib目錄中,將5個tld文件釋放到web\WEB-INF目錄中,在web\WEB-INF目錄中新建struts-config.xml,內(nèi)容如下:

          <?xml version="1.0" encoding="ISO-8859-1" ?>

          <!DOCTYPE struts-config PUBLIC
                    "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
                    "http://struts.apache.org/dtds/struts-config_1_2.dtd"
          >

          <struts-config>

              
          <!-- ========== Data Source Configuration =============================== -->
              
          <data-sources />

              
          <!-- ========== Form Bean Definitions =================================== -->
              
          <form-beans />

              
          <!-- ========== Global Exception Definitions ============================ -->
              
          <global-exceptions />

              
          <!-- ========== Global Forward Definitions ============================== -->
              
          <global-forwards />

              
          <!-- ========== Action Mapping Definitions ============================== -->
              
          <action-mappings />

              
          <!-- ========== Controller Configuration ================================ -->

              
          <!-- ========== Message Resources Definitions =========================== -->
              
          <message-resources parameter="application" />

              
          <!-- ========== Plug Ins Configuration ================================== -->
              
          <plug-in className="net.sf.navigator.menu.MenuPlugIn">
                
          <set-property property="menuConfig" value="/WEB-INF/menu-config.xml"/>
              
          </plug-in>

          </struts-config>

          posted @ 2008-08-05 14:52 gdufo| 編輯 收藏

          結(jié)合appfuse學習acegi

          1.web.xml的配置

          web.xml加入如下Filter的配置
          <filter
          >
            <
          filter-name>securityFilter</filter-name
          >
            <
          filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class
          >
            <
          init-param
          >
                <
          param-name>targetClass</param-name
          >
                 <
          param-value>org.acegisecurity.util.FilterChainProxy</param-value
          >
                  </
          init-param
          >
               </
          filter
          >


                    FilterToBeanProxy就是Acegi過濾器Bean的代理,功能是把http請求按順序分派給過濾器Bean;
          說明:Acegi對WEB應用的支持主要是依靠servlet 過濾器(filter) 來實現(xiàn)的。每一個http request都將被這┕ 似韃悴憷菇?并進行安全處理(包括認證和授權(quán))。針對不同的安全處理,Acegi提供了不同的過濾器。過濾器的配置信息理論上應位于web.xml,但是我們又希望把Acegi的過濾器配置信息放在SpringFramework的配置文件里(applicationContext-security-acegi.xml),從而實現(xiàn)對這些過濾器的“控制反轉(zhuǎn)”。解決這個問題的方法是采用Acegi提供的FilterToBeanProxy。FilterToBeanProxy顧名思義就是對Acegi過濾器Bean的代理,它的主要功能就是將http請求依次分派給對應的過濾器Bean。
          這個Filter需要一個參數(shù)targetClass(目標類),意思是說代理哪個bean,為它配置的值是FilterChainProxy,就是由這個FilterChainProxy類負責將需要代理的這些Filter組成Filter代理“鏈”(這些Filter在Security.xml這個配置文件里以bean的形式進行配置了)
          web.xml加入如下內(nèi)容
                     <filter-mapping>
                         <filter-name>securityFilter</filter-name>
                         <url-pattern>/j_security_check</url-pattern>
                     </filter-mapping>
                     <filter-mapping>
                         <filter-name>securityFilter</filter-name>
                         <url-pattern>*.html</url-pattern>
                     </filter-mapping>
                     <filter-mapping>
                         <filter-name>securityFilter</filter-name>
                         <url-pattern>*.jsp</url-pattern>
                     </filter-mapping>
          這個就好理解了,就是這樣的url都讓他被securityFilter這個過濾器過濾處理

          web.xml加入如下內(nèi)容
                   <context-param>
                       <param-name>contextConfigLocation</param-name>
                       <param-value>/WEB-INF/applicationContext-*.xml,/WEB-INF/security.xml</param-value>
                   </context-param>
          這個就是讓上下文載入/WEB-INF/applicationContext-*.xml,/WEB-INF/security.xml這些配置文件

          2.security.xml的配置
          首先加入filterChainProxy這個bean的定義,因為前面在web.xml配置的filter需要這個bean
                 <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
                     <property name="filterInvocationDefinitionSource">
                         <value>
                             CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                             PATTERN_TYPE_APACHE_ANT
                             /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor
                         </value>
                     </property>
                 </bean>
          CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定義了url比較前先轉(zhuǎn)為小寫, PATTERN_TYPE_APACHE_ANT定義了使用Apache ant的匹配模式
          這個bean將httpSessionContextIntegrationFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor這些bean組成了filter鏈,當發(fā)生需要web.xml配置的過濾器的對應的需要過濾的url的請求時,這些filter將會按照定義的順序被調(diào)用,這個過濾器的mapping是“/*”。
          既然配置了這些需要被調(diào)用的給filterChainProxy,那么就要對這些filter進行逐個配置,以便filterChainProxy能找到并調(diào)用他們。
          httpSessionContextIntegrationFilter的配置及作用
          <bean id="httpSessionContextIntegrationFilter"

                          class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"/>
          每次request前 HttpSessionContextIntegrationFilter從Session中獲取Authentication對象并組裝ContextHolder。ContextHolder主要用于存放SecureContext,包括用戶的權(quán)限信息,在request完后, 又把Authentication對象保存到Session中供下次request使用,此filter必須其他Acegi filter前使用,使之能跨越多個請求。
            

          authenticationProcessingFilter的配置及作用
                 <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
                     <property name="authenticationManager" ref="authenticationManager"/>
                     <property name="authenticationFailureUrl" value="/login.jsp?error=true"/> 登錄失敗的url
                     <property name="defaultTargetUrl" value="/"/>     登錄成功后的url
                     <property name="filterProcessesUrl" value="/j_security_check"/>     需要這個filter處理的url
                     <property name="rememberMeServices" ref="rememberMeServices"/>
                 </bean>
          該Filter負責處理登陸身份驗證。當接受到與filterProcessesUrl所定義相同的請求時,它會首先通過 AuthenticationManager來驗證用戶身份。如果驗證成功,則重定向到defaultTargetUrl所定義的成功登陸頁面。如果驗證失敗,則再從rememberMeServices中獲取用戶身份,若再獲取失敗,則重定向到authenticationFailureUrl所定義登陸失敗頁面。

          securityContextHolderAwareRequestFilter的配置及作用
          <bean id="securityContextHolderAwareRequestFilter" class="org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter"/>
          該Filter負責通過Decorate Model(裝飾模式),裝飾的HttpServletRequest對象。其Wapper是ServletRequest包裝類 HttpServletRequestWrapper的子類(SavedRequestAwareWrapper或 SecurityContextHolderAwareRequestWrapper),附上獲取用戶權(quán)限信息,request參數(shù),headers, Date headers 和 cookies 的方法。
          在appfuse中使用的struts-menu.xml對用戶菜單權(quán)限的控制參數(shù)role進行配置時必須結(jié)合該Filter,因為該filter有isUserInRole方法,而struts menu也是通過request.isUserInRole方法來控制菜單是否顯示的
          RememberMeProcessingFilter的配置及作用
                 <bean id="rememberMeProcessingFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
                                <property name="authenticationManager" ref="authenticationManager" />
                                <property name="rememberMeServices" ref="rememberMeServices" />
                 </bean>
          該Filter負責在用戶登錄后在本地機上記錄用戶cookies信息,免除下次再次登陸。檢查AuthenticationManager 中是否已存在Authentication對象,如果不存在則會調(diào)用RememberMeServices的aotoLogin方法來從cookies中獲取Authentication對象

          AnonymousProcessingFilter的配置及作用
                 <bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
                     <property name="key" value="anonymous"/>
                     <property name="userAttribute" value="anonymous,ROLE_ANONYMOUS"/>
                 </bean>
          該Filter負責為當不存在任何授權(quán)信息時,自動為Authentication對象添加userAttribute中定義的匿名用戶權(quán)限

          ExceptionTranslationFilter的配置及作用
          <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
                 <property name="authenticationEntryPoint">
                     <bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
                          <property name="loginFormUrl" value="/login.jsp" />
                          <property name="forceHttps" value="false" />
                     </bean>
                  </property>
                  <property name="accessDeniedHandler">
                      <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
                          <property name="errorPage" value="/accessDenied.jsp" />
                       </bean>
                   </property>
          </bean>
          該過濾器負責處理各種異常,然后重定向到相應的頁面中。
          filterInvocationInterceptor的配置及作用
                <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
                    <property name="authenticationManager" ref="authenticationManager"/>
                    <property name="accessDecisionManager" ref="accessDecisionManager"/>
                    <property name="objectDefinitionSource">
                        <value>
                            PATTERN_TYPE_APACHE_ANT
                            /signup.html*=ROLE_ANONYMOUS,admin,user
                            /passwordHint.html*=ROLE_ANONYMOUS,admin,user
                            /**/*.html*=admin,user
                            /clickstreams.jsp*=admin
                        </value>
                    </property>
                </bean>

          <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">  
              <property name="authenticationManager" ref="authenticationManager"/>         認證服務   
              <property name="accessDecisionManager">  
               <bean class="org.acegisecurity.vote.AffirmativeBased">  
                <property name="allowIfAllAbstainDecisions" value="false"/>  
                <property name="decisionVoters">  
                 <list>  
                  <bean class="org.acegisecurity.vote.RoleVoter">  
                                <property name="rolePrefix" value=""/>           //這里定義數(shù)據(jù)庫中存放的角色和我們在這里聲明的角色間是否需要加個前綴?這里沒加   
                            </bean>  
                 </list>  
                </property>  
               </bean>  
              </property>  
              <property name="objectDefinitionSource">  
                        <value>  
                            PATTERN_TYPE_APACHE_ANT   
                             
                            /admin.htm*=a           這里就是數(shù)據(jù)庫中對應的tyep a   
                            /student*=s             由于沒有前綴和數(shù)據(jù)庫里一樣   
                            /teacher*=t   
                        </value>  
                    </property>  
          </bean>
          按照多數(shù)的ACEGI的配置文檔,權(quán)限表的權(quán)限標識多以AUTH_或ROLE_開頭,那么如果你的表中是這么存儲的就可以在rolePrefix處配置一個開頭標識(其實這僅僅是個標識,沒有任何實際意義,appfuse就沒有這么配置)

          該過濾器會首先調(diào)用AuthenticationManager判斷用戶是否已登陸認證,如還沒認證成功,則重定向到登陸界面。認證成功,則并從 Authentication中獲取用戶的權(quán)限。然后從objectDefinitionSource屬性獲取各種URL資源所對應的權(quán)限。最后調(diào)用 AccessDecisionManager來判斷用戶所擁有的權(quán)限與當前受保華的URL資源所對應的權(quán)限是否相匹配。如果匹配失敗,則返回403錯誤 (禁止訪問)給用戶。匹配成功則用戶可以訪問受保護的URL資源。

          posted @ 2008-08-04 23:56 gdufo 閱讀(505) | 評論 (0)編輯 收藏

          sitemesh應用Decorator模式

          sitemesh應用Decorator模式,用filter截取request和response,把頁面組件head,content,banner結(jié)合為一個完整的視圖。通常我們都是用include標簽在每個jsp頁面中來不斷的包含各種header, stylesheet, scripts and footer,現(xiàn)在,在sitemesh的幫助下,我們可以開心的刪掉他們了。如下圖,你想輕松的達到復合視圖模式,那末看完本文吧。

          一、在WEB-INF/web.xml中copy以下filter的定義:

          <?xml version="1.0" encoding="GBK"?>
          <web-app 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"
          version="2.4">

          <filter>
            <filter-name>sitemesh</filter-name>
               <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
            </filter>

            <filter-mapping>
               <filter-name>sitemesh</filter-name>
               <url-pattern>/*</url-pattern>
            </filter-mapping>

          </web-app>

          二、copy所需sitemesh-2.3.jar到WEB-INF\lib下。(這里可以下載http://www.opensymphony.com/sitemesh/)

          三、
          建立WEB-INF/decorators.xml描述各裝飾器頁面。

          <decorators defaultdir="/decorators">
                                         <decorator name="main" page="main.jsp">
                                             <pattern>*</pattern>
                                         </decorator>
                                  </decorators>

            上面配置文件指定了裝飾器頁面所在的路徑,并指定了一個名為main的裝飾器,該裝飾器默認裝飾web應用根路徑下的所有頁面。

          四、
          建立裝飾器頁面 /decorators/main.jsp

        1. <%@ page contentType="text/html; charset=GBK"%>
          <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> <html>
                <head>
                    <title><decorator:title default="裝飾器頁面..." /></title>
                    <decorator:head />
                </head>
               <body>
                  sitemesh的例子<hr>
                  <decorator:body />
                  <hr>chen56@msn.com
              </body>
          </html>
           

          五、建立一個的被裝飾頁面 /index.jsp(內(nèi)容頁面)

        2. <%@ page contentType="text/html; charset=GBK"%>
                                  <html>
                                       <head>
                                         <title>Agent Test</title>
                                       </head>
                                       <body>
                                         <p>本頁只有一句,就是本句.</p>
                                       </body>
                                  </html>

            最后訪問index.jsp,將生成如下頁面:

                而且,所有的頁面也會如同index.jsp一樣,被sitemesh的filter使用裝飾模式修改成如上圖般模樣,卻不用再使用include標簽。



          1. 裝飾器     decorator概念
                為了建立可復用的web應用程序,一個通用的方法是建立一個分層系統(tǒng),如同下面一個普通的web應用:
            • 前端:JSP和Servlets,或jakarta的velocity 。。。
            • 控制層框架 Controller : (Struts/Webwork)
            • 業(yè)務邏輯 Business :主要業(yè)務邏輯
            • 持久化框架 :hibernate/jdo

                可糟糕的是前端的頁面邏輯很難被復用,當你在每一個頁面中用數(shù)之不盡的include來復用公共的header, stylesheet, scripts,footer時,一個問題出現(xiàn)了-重復的代碼,每個頁面必須用copy來復用頁面結(jié)構(gòu),而當你需要創(chuàng)意性的改變頁面結(jié)構(gòu)時,災難就愛上了你。

                 sitemesh通過filter截取request和response,并給原始的頁面加入一定的裝飾(可能為header,footer...),然后把結(jié)果返回給客戶端,并且被裝飾的原始頁面并不知道sitemesh的裝飾,這也就達到了脫耦的目的。

                 據(jù)說即將新出臺的Portlet規(guī)范會幫助我們標準的實現(xiàn)比這些更多更cool的想法,但可憐的我還不懂它到底是一個什末東東,有興趣的人可以研究
            jetspeed,或JSR (Java Specification Request) 168,但我想sitemesh如此簡單,我們不妨先用著。

             

            讓我們看看怎樣配置環(huán)境
                除了要copy到WEB-INF/lib中的sitemesh.jar外,還有2個文件要建立到WEB-INF/:
            • sitemesh.xml (可選)  
            • decorators.xml

            sitemesh.xml 可以設置2種信息:

            Page Parsers :負責讀取stream的數(shù)據(jù)到一個Page對象中以被SiteMesh解析和操作。(不太常用,默認即可)

            Decorator Mappers : 不同的裝飾器種類,我發(fā)現(xiàn)2種比較有用都列在下面。一種通用的mapper,可以指定裝飾器的配置文件名,另一種可打印的裝飾器,可以允許你當用http://localhost/aaa/a.html?printable=true方式訪問時給出原始頁面以供打印(免得把header,footer等的花哨的圖片也搭上)

            (但一般不用建立它,默認設置足夠了:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):

            范例:

            <sitemesh>
                 <page-parsers>
                   <parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
                   <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
                   <parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
                 </page-parsers>

                 <decorator-mappers>
                   <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
                     <param name="config" value="/WEB-INF/decorators.xml" />
                   </mapper>
                     <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
                        <param name="decorator" value="printable" />
                        <param name="parameter.name" value="printable" />
                                <param name="parameter.value" value="true" />
                     </mapper>
              
            </decorator-mappers>
            </sitemesh>

            decorators.xml :定義構(gòu)成復合視圖的所有頁面構(gòu)件的描述(主要結(jié)構(gòu)頁面,header,footer...),如下例:

            <decorators defaultdir="/decorators">
                 <decorator name="main" page="main.jsp">
                   <pattern>*</pattern>
                 </decorator>
                 <decorator name="printable" page="printable.jsp" role="customer" webapp="aaa" />
            </decorators>
            • defaultdir: 包含裝飾器頁面的目錄
            • page : 頁面文件名
            • name : 別名
            • role : 角色,用于安全
            • webapp : 可以另外指定此文件存放目錄
            • Patterns : 匹配的路徑,可以用*,那些被訪問的頁面需要被裝飾。

             

            最重要的是寫出裝飾器本身(也就是那些要復用頁面,和結(jié)構(gòu)頁面)。
                其實,重要的工作就是制作裝飾器頁面本身(也就是包含結(jié)構(gòu)和規(guī)則的頁面),然后把他們描述到decorators.xml中。

                讓我們來先看一看最簡單的用法:其實最常用也最簡單的用法就是我們的hello例子,面對如此眾多的技術(shù),我想只要達到功能點到為止即可,沒必要去研究太深(除非您有更深的需求)。

            <%@ page contentType="text/html; charset=GBK"%>
                                        <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
                                        <html>
                                             <head>
                                               <title><decorator:title default="裝飾器頁面..." /></title>
                                               <decorator:head />
                                             </head>
                                             <body>
                                               sitemesh的例子<hr>
                                               <decorator:body />
                                               <hr>chen56@msn.com
                                             </body>
                                        </html>
                                        

            我們在裝飾器頁面只用了2個標簽:

            <decorator:title default="裝飾器頁面..." />       : 把請求的原始頁面的title內(nèi)容插入到<title></title>中間。

            <decorator:body /> : 把請求的原始頁面的body內(nèi)的全部內(nèi)容插入到相應位置。

            然后我們在decorator.xml中加入以下描述即可:

            <decorator name="main" page="main.jsp">
                   <pattern>*</pattern>
            </decorator>

            這樣,請求的所有頁面都會被重新處理,并按照main.jsp的格式重新展現(xiàn)在你面前。

             

            讓我們看看更多的用法。(抄襲sitemesh文檔)
            以下列著全部標簽:
            Decorator Tags Page Tags
            被用于建立裝飾器頁面. 被用于從原始內(nèi)容頁面訪問裝飾器.
            <decorator:head />
            <decorator:body />
            <decorator:title />
            <decorator:getProperty />
            <decorator:usePage />
            <page:applyDecorator />
            <page:param
             

            <decorator:head />

            插入原始頁面(被包裝頁面)的head標簽中的內(nèi)容(不包括head標簽本身)。

            <decorator:body />

            插入原始頁面(被包裝頁面)的body標簽中的內(nèi)容。

            <decorator:title [ default="..." ] />

            插入原始頁面(被包裝頁面)的title標簽中的內(nèi)容,還可以添加一個缺省值。

            例:

            /decorator/main.jsp中 (裝飾器頁面): <title><decorator:title default="卻省title-hello"     /> - 附加標題</title>

            /aaa.jsp中 (原始頁面):<title>aaa頁面</title>

            訪問/aaa.jsp的結(jié)果:<title>aaa頁面 - 附加標題</title>

            <decorator:getProperty property="..." [ default="..." ] [ writeEntireProperty="..." ]/>

            在標簽處插入原始頁面(被包裝頁面)的原有的標簽的屬性中的內(nèi)容,還可以添加一個缺省值。

            sitemesh文檔中的例子很好理解:
            The decorator: <body bgcolor="white"<decorator:getProperty property="body.onload" writeEntireProperty="true" />>
            The undecorated page: <body onload="document.someform.somefield.focus();">
            The decorated page: <body bgcolor="white" onload="document.someform.somefield.focus();">

            注意,writeEntireProperty="true"會在插入內(nèi)容前加入一個空格。

            <decorator:usePage id="..." />
            象jsp頁面中的<jsp:useBean>標簽一樣,可以使用被包裝為一個Page對象的頁面。 (懶的用)

            例:可用<decorator:usePage id="page" /><%=page.getTitle()%>達到<decorator:title/>的訪問結(jié)果。

            <page:applyDecorator name="..." [ page="..." title="..." ] >
            <page:param name="..."> ... </page:param>
            <page:param name="..."> ... </page:param>
            </page:applyDecorator>

            應用包裝器到指定的頁面上,一般用于被包裝頁面中主動應用包裝器。這個標簽有點不好理解,我們來看一個例子:

            包裝器頁面 /decorators/panel.jsp:<p><decorator:title /></p>     ... <p><decorator:body /></p>
                 并且在decorators.xml中有<decorator name="panel" page="panel.jsp"/>

            一個公共頁面,即將被panel包裝:/public/date.jsp:  
                 ... <%=new java.util.Date()%>     ...<decorator:getProperty property="myEmail" />

            被包裝頁面 /page.jsp :
                 <title>page的應用</title>
                 .....  

                 <page:applyDecorator name="panel" page="/_public/date.jsp" >
                   <page:param name="myEmail"> chen_p@neusoft.com </page:param>
                 </page:applyDecorator>

            最后會是什末結(jié)果呢?除了/page.jsp會被默認的包裝頁面包裝上header,footer外,page.jsp頁面中還內(nèi)嵌了date.jsp頁面,并且此date.jsp頁面還會被panel.jsp包裝為一個title加body的有2段的頁面,第1段是date.jsp的title,第2段是date.jsp的body內(nèi)容。

            另外,page:applyDecorator中包含的page:param標簽所聲明的屬性值還可以在包裝頁面中用decorator:getProperty標簽訪問到。


            可打印的界面裝飾
                 前面說過有1種可打印的裝飾器,可以允許你當用http://localhost/aaa/a.html?printable=true方式訪問時,應用其他的裝飾器(自己指定),給出原始頁面以供打印(免得把header,footer等的花哨的圖片也搭上)。

            讓我們來看一看怎樣實現(xiàn)他:

            1.首先在WEB-INFO/sitemesh.xml中設置:
                 <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
                   <param name="decorator" value="printable" />
                   <param name="parameter.name" value="printable" />
                   <param name="parameter.value" value="true" />
                 </mapper>
            這樣就可以通過?printable=true來使用名為printable的裝飾器,而不是用原來的裝飾器。

            2.在WEB-INFO/decorators.xml中定義相應的printable裝飾器
                 <decorator name="printable" page="printable.jsp"/>

            3.最后編寫printable裝飾器/decorators/printable.jsp

            <%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
            <html>
            <head>
                 <title><decorator:title /></title>
                 <decorator:head />
            </head>
            <body>

                 <h1><decorator:title /></h1>
                 <p align="right"><i>(printable version)</i></p>

                 <decorator:body />

            </body>
            </html>

            這樣就可以讓一個原始頁面通過?printable=true開關(guān)來切換不同的裝飾器頁面。

             

            中文問題
            由于sitemesh內(nèi)部所使用的缺省字符集為iso-8859-1,直接使用會產(chǎn)生亂碼,我們可以通過以下方法糾正之:
            • 方法1:可以在您所用的application server的配置文件中找一找,有沒有設置encoding或charset的項目,然后設成gbk或gb2312即可
            • 方法2:這也是我們一直使用的方法。
              1.在每一個jsp頁里設置: <%@ page contentType="text/html; charset=gbk"%> 來告訴server你所要求的字符集。
              2.在每個jsp頁的head中定義:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 來告訴瀏覽器你所用的字符集。
            總結(jié):使用sitemesh最通常的途徑:

            1.配置好環(huán)境,

            2.在WEB-INFO/decroators.xml中描述你將建立的包裝器。

            3.開發(fā)在decroators.xml中描述的包裝器,最好存放在/_decorators目錄下

            4.ok ,可以看看辛勤的成果了 :)

            posted @ 2008-08-04 17:21 gdufo| 編輯 收藏

            Appfuse 中的 Struts2中參數(shù)設置

            Appfuse 中的 Struts2中參數(shù)設置

                <constant name="struts.devMode" value="false"/>
                
            <constant name="struts.i18n.encoding" value="UTF-8"/>
                
            <constant name="struts.action.extension" value="html"/><!--這里將默認的.action 改成 .html結(jié)尾-->
                
            <constant name="struts.objectFactory" value="spring"/>
                
            <constant name="struts.custom.i18n.resources" value="ApplicationResources,errors"/>
                
            <constant name="struts.multipart.maxSize" value="2097152"/>
                
            <constant name="struts.ui.theme" value="css_xhtml"/>
                
            <constant name="struts.codebehind.pathPrefix" value="/WEB-INF/pages/"/>
                
            <constant name="struts.enable.SlashesInActionNames" value="true"/>
            ...........

            posted @ 2008-08-04 15:26 gdufo| 編輯 收藏

            struts2 配置(struts.properties,strtus.xml)

            1.導入STRUTS2-core-2.0.X.jar,xwork-2.0.X.jar,ognl-2.6.x.jar
            2.配置web.xml文件
            加入org.apache.struts2.dispatcher.FilterDispatcher過濾器的配置

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

            FilterDispatcher是STRUTS2核心控制器。負責攔截所有的用戶請求。

            用戶請求的到達時,F(xiàn)ilter過濾用戶請求。如果用戶請求以action結(jié)尾,請求將被轉(zhuǎn)入struts2框架處理。

            <filter>可以加入以下參數(shù)
              
            <init-param>
                
            <param-name>config</param-name>
                  
            <param-value>以逗號隔開的多個struts配置文件的名,沒有XML后綴</param-value>

              
            </init-param>

              
            <init-param>
                 
            <param-name>actionPackages</param-name>
                  
            <param-value>
            org.apache.struts2.showcase.person包空間名,多個用逗號隔開
            </param-value>
            </init-param>

            <init-param>

                     
            <param-name>configProviders</param-name>

            <param-value>

            一個或多個實現(xiàn)ConfigurationProvider接口的配置提供類的類全名,多個類用逗號隔開

            </param-value>

            </init-param>

             

            3.在classes下加入struts.xml文件

            Struts.xml文件用來配置action,攔截器等。

             

            <?xml version=”1.0” encoding=”GBK”?>

            <!—DTD信息-->

            <!DOCTYPE struts PUBLIC
            “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
            “http://struts.apache.org/dtds/struts-2.0.dtd”
            >
            <struts>
            <package name=”packagename” extends=”struts-default”>
            <action name=”Login” class=”lee.LoginAction”>
            <result name=”input”>login.jsp</result>
            </action>
            </package>
            </struts>

            i.Struts-default.xml配置文件位于struts-core-2.0.x.jar包中。是struts2最核心的配置文件。

            ii.從中可以看到整個的struts的核心功能的全貌.

            iii.Struts可以在result元素中指定相應的type屬性(默認jsp).從而支持其他的視圖技術(shù)。

            v.包可以繼承其他的包。包可以定義命名空間,用來區(qū)別其他配置文件中的相同的包名。

            vi.包可以定義成abstract=true 這樣包就只能被繼承,不能被框架直接實例化。

            vii.struts可以將一個struts.xml文件分解成多個配置文件,然后在struts.xml文件中包含其他配置文件。<struts><include file=”struts-partOne.xml”/></struts>

            注意:也可以在web.xml中配置filter時加入config參數(shù)來加載多個配置文件。

             

            3.struts.properties配置文件

            該文件定義了struts2框架的大量屬性。只要將該文件放在web應用的CLASSPATH下,struts2框架就會自動加載。

            stuts.locale

            指定Web應用的默認Locale

            struts.i18n.encoding

            指定應用的默認編碼相當于調(diào)用HttpServletRequest的setCharacterEncoding方法。

            struts.custom.i18n.resources

            指定struts應用所需要的國際化資源文件的名稱(message_zh_cn.properties只需要指定名為message)

            struts.mutipart.parser

            該屬性處理mutipart/form-data的MIME類型請求的框架(cos|pell|jakarta)默認是jakarta(common-fileupload)

            struts.multipart.saveDir

            指定上傳文件的臨時保存路徑,默認是javax.servlet.context.tempdir;

            struts.multipart.maxSize

            指定文件上傳中整個請求內(nèi)容允許的最大字節(jié)數(shù)。

            struts.action.extension

            指定Struts處理請求后綴,默認是action(login.action)

            struts.serve.static.browserCache

            指定瀏覽器是否緩存靜態(tài)內(nèi)容

            struts.enable.SlashesInActionNames

            該屬性設置struts2是否允許在Action名中使用斜線,該屬性的默認值是false。

            struts.devMode

            設置struts2是否使用開發(fā)模式(true|false)

            struts.dispatcher.parametersWorkaround

            對于不支持HttpServlertRequest調(diào)用getParameterMap()方法的服務器,可以設置該屬性值為true

            來解決該問題。不支持getParameterMap()方法的服務器:weblogic,orion,oc4j

             struts.i18n.reload

            設置是否每次HTTP請求到達時,系統(tǒng)都重新加載資源文件。默認false

            struts.ui.theme

            設置視圖標簽的默認主題。默認是xhtml

            struts.ui.templateDir

            設置視圖主題所需模板文件的位置,默認是template,默認加載template路徑下的模板文件。

            struts.ui.templateSuffix

            指定模板文件的后綴(ftl|vm|jsp)

            struts.configuration.xml.reload

             設置當struts.xml文件改變后,系統(tǒng)是否自動重新加載。

            struts.configuration.files

            指定默認加載的配置文件,默認值是:struts-default.xml,struts-plugin.xml,struts.xml

            struts.objectFactory

            指定struts2默認的ObjectFactoryBean,該屬性默認是spring

            struts.objectFactory.spring.autoWire

             指定Spring框架自動裝配模式,默認是name.即根據(jù)Bean的name屬性自動裝配。

            struts.objectFactory.spring.userClassCache

             指定spring框架是否緩存Bean實例。默認是true

            struts.objectTypeDeterminer (tiger|notiger)


             

            posted @ 2008-08-04 14:03 gdufo| 編輯 收藏

            忘記Mysql密碼

            1.    先殺掉mysqld的進程:
            >kill –TERM mysqld
            2. 使用skip-grant-tables這個選項啟動MySQL: 
               
            /usr/local/mysql/bin/mysqld_safe –skip-grant-tables    
            2.    登錄修改密碼: 
            >mysql –u root –p
            Mysql 
            >use mysql
            Mysql
            >update user set ….
            Mysql
            >flush privileges;
            Mysql
            >exit
              
            3.    關(guān)掉MySQL 
            >mysqladmin – u root –p shutdown
            5. 啟動MySQL 

            posted @ 2008-07-04 13:08 gdufo| 編輯 收藏

            Linux常用到的命令

             

            ==手支啟動Apache2

            shell> /usr/local/apache2/bin/apachectl start <=激活

            apachectl 還有其它的參數(shù)如下:
            apachectl stop
            :停止 WWW 的服務;
            apachectl restart
            :重新激活 WWW 的服務,這個指令比較常用在你修改了 apache 的參數(shù)后,重新激活用的。
            apachectl status
            :偵測 WWW 的狀態(tài)。

            ==刪除目錄

            shell> rm –rf 目錄

            ==新建目錄

            shell> mrdir 目錄

            ==Copy文件

            shell>cp  /home/weixubin/mysqltest.php  /usr/local/apache2/htdocs

            ==Copy目錄

            shell>cp -R /home/xubin.wei/crm /usr/local/apache/htdocs/crm

            ==Copy目錄下指定格式

            shell>cp /home/test/*.zip /usr/pwservices/

            ==文件所屬

            shell>chown -R nobody.nobody /usr/local/apache2/htdocs/crm

            ==更改文件屬性

            shell>chmod -R 777 /usr/local/apache2/htdocs/crm

             

            ==查看是否已安裝程序

            shell>rpm –q mysql

            ==刪除一程序

            shell>rpm –e mysql-3.23

             

             

            ==文件夾 Rename

            shell>mv  apache-ant-1.5.4  ant

            ==啟動postgresql

            shell> /usr/local/pgsql/bin/postmaster –I (小寫) -D /home/postgre/data

            ==偵測端口是否被占用

            shell>netstat -an | grep 389

            ==從根目錄查找文件,并顯示。

            Shell>find / -name alert*.log –print

             

            ==查看進程

            Shell> ps -ef | grep mysql

             

            ==查看磙盤空間大小

            Shell>df h

             

            ==修復

            Shell>fsck t ext2 /dev/ext2

             
             

            ==定位文件

            shell> locate my.cnf

            shell>vi my.cnf 編輯文件

            編輯:Insert

            命令狀態(tài);ESC

            :wq保存

             

            ==增加組

            shell>groupadd mysql

             

            ==查看全路徑

            shell>pwd

            posted @ 2008-07-04 13:07 gdufo| 編輯 收藏

            僅列出標題
            共19頁: First 上一頁 11 12 13 14 15 16 17 18 19 下一頁 

            導航

            統(tǒng)計

            常用鏈接

            留言簿(6)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            收藏夾

            Hibernate

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            主站蜘蛛池模板: 福州市| 大兴区| 佛山市| 乌海市| 当涂县| 平和县| 察哈| 屏南县| 张掖市| 新营市| 新昌县| 太康县| 小金县| 麟游县| 阿图什市| 进贤县| 丽水市| 图们市| 凌海市| 苍山县| 岱山县| 汾西县| 江华| 宾阳县| 嘉祥县| 东海县| 靖远县| 沁阳市| 石嘴山市| 眉山市| 西城区| 信宜市| 客服| 沂南县| 隆林| 松原市| 汝城县| 汉源县| 渭南市| 台北县| 青龙|