dwr整合ssh 的annotation配置方式

前端之家收集整理的这篇文章主要介绍了dwr整合ssh 的annotation配置方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以下是需要注意的文件
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>character encoding filter</filter-name>
    <filter-class>
   org.springframework.web.filter.CharacterEncodingFilter
  </filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>character encoding filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>flushMode</param-name>
      <param-value>AUTO</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/Shopping_addShopps</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.bohua.filter.RequestFilter</filter-class>
    <init-param>
      <param-name>notCheckUrl</param-name>
      <param-value>index.jsp</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/User_addUser</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>accountFilter</filter-name>
    <filter-class>com.bohua.filter.AccountFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>accountFilter</filter-name>
    <url-pattern>/Shopping_account</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
  <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>
  <!-- dwr的注解 -->
   <servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>
   <!-- org.directwebremoting.servlet.DwrServlet -->
     org.directwebremoting.spring.DwrSpringServlet     
    </servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>activeReverseAjaxEnabled</param-name>
      <param-value>true</param-value>
    </init-param> 

    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/cyqdwr/*</url-pattern>
  </servlet-mapping>
</web-app>

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    <!-- 开启annotation -->
    <context:annotation-config />

    <!-- DWR 配置开始 -->
    <!-- <dwr:configuration /> 如果你想配置Spring容器之外的类,就需要它了。 -->
    <dwr:configuration>
    <dwr:convert type="bean" class="com.bohua.beans.Menu"></dwr:convert> 
    </dwr:configuration><!-- 必须要configuration -->
    <!-- 部署项目时,请把debug设为false --> 
    <dwr:controller id="dwrController" debug="true" />
    <!-- 要求DWR在Spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类 -->
    <dwr:annotation-config id="dwr_id"/>      
    <!-- 要求DWR将util.js和engine.js映射到dwrController --> 
    <dwr:url-mapping />      
    <!-- DWR 配置结束 -->

    <!-- 自动扫描与装配bean -->
    <context:component-scan base-package="com.bohua"></context:component-scan>
    <!-- 导入外部的properties文件 -->
    <bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!--xml配置model -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
        <property name="dataSource" ref="dataSource" />

        <property name="mappingLocations">
            <value>classpath:/com/bohua/beans/*.hbm.xml </value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.sqlServerDialect
                </prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
            </props>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="required" />
            <tx:method name="get*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 定义切面 -->
    <aop:config>
        <aop:aspect id="Log" ref="aspectBean">
            <!--配置com.spring.service包下所有类或接口的所有方法 -->
            <aop:pointcut id="businessService" expression="execution(* com.bohua.service..*.*(..))" />
            <aop:before pointcut-ref="businessService" method="doBefore" />
            <aop:after pointcut-ref="businessService" method="doAfter" />
            <aop:around pointcut-ref="businessService" method="doAround" />
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex" />
        </aop:aspect>
    </aop:config>
    <bean id="aspectBean" class="com.bohua.aop.Log" />


    <!-- hibernate template是spring提供给使用者的一个便捷类搜索, 可以方便操作数据库,无需自己创建sessionFactory获取session -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

antion

package com.bohua.action;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.swing.Spring;

import org.directwebremoting.Browser;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringConfigurator;
import org.directwebremoting.spring.SpringContainer;
import org.directwebremoting.spring.SpringCreator;
import org.directwebremoting.ui.dwr.Util;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.SpringTransactionAnnotationParser;

import com.bohua.beans.Menu;
import com.bohua.beans.UserGroup;
import com.bohua.service.EnterService;

@Controller
//@Scope("prototype")
@RemoteProxy(creator=SpringCreator.class,name="menuSelect")
public class EnterAction {

    private ArrayList<UserGroup> groups;
    private ArrayList<Menu> menus;
    private ArrayList<Menu> allMenu;
    private EnterService enterService;


    public ArrayList<Menu> getMenus() {
        return menus;
    }
    public void setMenus(ArrayList<Menu> menus) {
        this.menus = menus;
    }
    public ArrayList<UserGroup> getGroups() {
        return groups;
    }
    public void setGroups(ArrayList<UserGroup> groups) {
        this.groups = groups;
    }
    public EnterService getEnterService() {
        return enterService;
    }
    @Resource
    public void setEnterService(EnterService enterService) {
        this.enterService = enterService;
    }

    public String addGroup(){
        enterService.addGroup(groups);
        return "addGroup";
    }
    public String addMenu(){
        allMenu=new ArrayList<Menu>();
        allMenu=enterService.addMenu(menus);
        return "addMenu";
    }
    @RemoteMethod
    public String flushMenu(){
        allMenu=new ArrayList<Menu>();
        allMenu=enterService.findAllMenu();
        System.out.println(allMenu.size()+" ffffff");
        //ScriptSession scriptSession =WebContextFactory.get().getScriptSession();
        String page = ServerContextFactory.get().getContextPath()+ "/EnterMenu.jsp";  
        Browser.withPage(page,new Runnable() {  
            public void run() {  
                Util.removeAllOptions("menuSelect");
                Util.addOptions("menuSelect",allMenu,"menuName");
            }  
        });  
        return "addMenu";
    }
}

jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'EnterGroup.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">

<script type='text/javascript' src='cyqdwr/engine.js'></script>
<script type='text/javascript' src='cyqdwr/util.js'></script>
<script type='text/javascript' src='cyqdwr/interface/menuSelect.js'></script>
<script type="text/javascript"> var i = 1; function addRow() { var tdValue = document.getElementById("td1").firstChild.nodeValue; var oTab = document.getElementById("myTab"); var oTr = document.createElement('tr');//创建一个tr //创建第一个td var otd = document.createElement('td'); otd.innerHTML = tdValue; oTr.appendChild(otd);//将td插入tr //创建第二个td var otd = document.createElement('td'); otd.innerHTML = "<input type='text' name='menus.groupName'>"; oTr.appendChild(otd);//将td插入tr oTab.tBodies[0].appendChild(oTr);//将整个tr插入到表格中 i++; } </script>
 <script type="text/javascript"> function flashSel(){ menuSelect.flushMenu(); } </script>
</head>

<body onload="dwr.engine.setActiveReverseAjax(true),flashSel();">
    <span>菜单录入界面</span>
    <form action="Enter_addMenu" method="get">
        <table id="myTab">
            <tr>
                <td id="td1">菜单名称</td>
                <td><input type="text" name="menus.menuName"></td>
            </tr>
            <tr>
                <td id="td1">菜单URL</td>
                <td><input type="text" name="menus.menuURL"></td>
            </tr>
            <tr>
                <td id="td1">菜单</td>
                <td><select id="menuSelect">

                </select> <input type="text" name="menus.menu"></td>
            </tr>
        </table>
        <input type="button" value="增加菜单" onclick="addRow()"> <input  type="submit" value="录入" onclick="sub()">
    </form>
</body>
</html>
  • 简单解释一下这些配置

    • 要求DWR在Spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。

    • 要求DWR将util.js和engine.js映射到dwrController

    • 定义dwrController

    •如果你想配置Spring容器之外的类,就需要它了。

猜你在找的Ajax相关文章