dwr整合ssh 的annotation配置方式

原创 2015年07月11日 09:58:06

以下是需要注意的文件:
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>

action

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容器之外的类,就需要它了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

dwr整合ssh的xml配置方式

struts2.0,spring2.5,hibernate3.0,dwr3.0.web.xml

ssh整合(annotation方式)

  • 2017年12月04日 15:36
  • 20.31MB
  • 下载

SSH整合(一)hibernat非Annotation配置

开发环境:MyEclipse6.5 + JDK1.6.0_33 + tomcat 6.0.32 + struts2.3.8 + spring3.2.1 + hibernate4.1.9   项目结...
  • moke44
  • moke44
  • 2013年02月01日 14:37
  • 357

Spring2.5.6+Struts2+Hibernate3.6.7+DWR3.0 注解(Annotation)式配置

Annotation(注解)是JDK5.0及以后版本引入的。它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查。注解的使用非常简单,在代码中只需要以‘@注解名’这种形式使用即可。除了符号'...

小试Annotation来配置DWR

DWR2.0以上版本支持通过配置Annotation的方式来配置DWR,可以完全抛弃dwr.xml.1.和用dwr.xml的配置稍稍有一些不一样。来看看:xml version="1.0" encod...
  • haidage
  • haidage
  • 2011年07月27日 13:30
  • 542

Dwr整合Spring使用Annotation

下面描述的是没有用SpringMVC的情况,按照官方的描述,用SpringMVC的配置又是不一样的! 首先在Spring的ApplicationContext配置文件里面的beans声明中加入如...
  • elim168
  • elim168
  • 2014年10月28日 09:09
  • 7946
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dwr整合ssh 的annotation配置方式
举报原因:
原因补充:

(最多只允许输入30个字)