DWR是Java调用AJAX的框架,通过将Java的对象中的方法映射成为js中对应的对象和方法,从而使调用js的方法同时会调用Java中的对应方法,并将返回的结果处理成为js对象或数组,返回给回调函数来处理。
DWR可以很容易的与Spring框架结合,但DWR对于hibernate的关系映射不支持。
(1)加入Spring支持
(2)加入Hibernate支持
(3)修改Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/info_users"> </property> <property name="username" value="root"></property> <property name="password" value="mysqladmin"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean></beans> |
(4)加入Struts支持
(5)在struts-config.xml中加入Spring的插件配置
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"> </controller> <message-resources parameter="mldn.lin.struts.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" /> </plug-in> |
(6)加入日志文件
加到src下
(1)删除冲突jar包
删除asm-2.2.3.jar、ehcache-1.1.jar
(2)加入数据库公共连接池支持jar包
加入commons-pool.jar支持jar包
将dwr.jar拷贝到项目的lib下,将dwr.xml拷贝到项目的WEB-INF下
在web.xml中加入dwr的Servlet配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>debug</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>/dwr
public class Users implements java.io.Serializable {
// Fields
private String userid;
private String realName;
private String password;
private String photo;
// Constructors
public Users() { }
public Users(String userid, String realName, String password, String photo) { this.userid = userid; this.realName = realName; this.password = password; this.photo = photo; }
// Property accessors
public String getUserid() { return this.userid; }
public void setUserid(String userid) { this.userid = userid; }
public String getRealName() { return this.realName; }
public void setRealName(String realName) { this.realName = realName; }
public String getPassword() { return this.password; }
public void setPassword(String password) { this.password = password; }
public String getPhoto() { return this.photo; }
public void setPhoto(String photo) { this.photo = photo; }
} |
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="mldn.lin.pojo.Users" table="users" catalog="info_users"> <id name="userid" type="java.lang.String"> <column name="userid" length="20" /> <generator class="assigned" /> </id> <property name="realName" type="java.lang.String"> <column name="real_name" length="20" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="10" not-null="true" /> </property> <property name="photo" type="java.lang.String"> <column name="photo" length="50" not-null="true" /> </property> </class> </hibernate-mapping>
|
package mldn.lin.dao;
public interface UsersDAO { public boolean checkUserid(String userid) throws Exception; } |
package mldn.lin.dao.impl;
import mldn.lin.dao.UsersDAO; import mldn.lin.pojo.Users;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImpl extends HibernateDaoSupport implements UsersDAO {
public boolean checkUserid(String userid) throws Exception { // TODO Auto-generated method stub Users users = (Users) this.getHibernateTemplate().get(Users.class, userid); return users != null ; } }
|
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/info_users"> </property> <property name="username" value="root"></property> <property name="password" value="mysqladmin"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>mldn/lin/pojo/Areaplus.hbm.xml</value> <value>mldn/lin/pojo/Area.hbm.xml</value> <value>mldn/lin/pojo/Users.hbm.xml</value></list> </property></bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean> <bean id="usersdaoimpl" class="mldn.lin.dao.impl.UsersDAOImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"></ref> </property> </bean> </beans> |
<?xml version="1.0" encoding="UTF-8"?> <!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-sources /> <form-beans > <form-bean name="registForm" type="mldn.lin.struts.form.RegistForm" />
</form-beans>
<global-exceptions /> <global-forwards /> <action-mappings > <action attribute="registForm" input="/regist.jsp" name="registForm" parameter="status" path="/regist" scope="request" type="mldn.lin.struts.action.RegistAction" />
</action-mappings>
<message-resources parameter="mldn.lin.struts.ApplicationResources" /> </struts-config>
|
package mldn.lin.struts.form;
import javax.servlet.http.HttpServletRequest; import mldn.lin.pojo.Users; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping;
public class RegistForm extends ActionForm {
private Users users=new Users();
private String photo;
private String status;
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; }
public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub }
public Users getUsers() { return users; }
public void setUsers(Users users) { this.users = users; }
public String getPhoto() { return photo; }
public void setPhoto(String photo) { this.photo = photo; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; } } |
package mldn.lin.struts.action;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import mldn.lin.dao.UsersDAO; import mldn.lin.struts.form.RegistForm;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction;
public class RegistAction extends DispatchAction { private UsersDAO usersdao;
public void setUsersdao(UsersDAO usersdao) { this.usersdao = usersdao; }
public ActionForward regist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { RegistForm registForm = (RegistForm) form;// TODO Auto-generated method stub return null; } } |
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>首页</title> </head>
<body> <center> <a href="regist.jsp">注册</a> </center> </body> </html> |
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base />
<title>regist.jsp</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body> <center> <html:form action=" regist.do " method="post" focus="users.userid"> <table border="0"> <tr> <td>用户ID:</td> <td><html:text property="users.userid" /></td> </tr> <tr> <td>真实姓名:</td> <td><html:text property="users.realName" /></td> </tr> <tr> <td>密码:</td> <td><html:password property="users.password" /></td> </tr> <tr> <td>头像:</td> <td><html:file property="photo" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit value="注册"/></td> </tr> </table> </html:form> </center> </body> </html:html>
|
在Spring管理中
<bean name="/regist" class="mldn.lin.struts.action.RegistAction"> <property name="usersdao"> <ref bean="usersdaoimpl"></ref> </property> </bean> |
修改dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> <dwr> <allow> <!-- 每一个<create>表示一个Java对象映射成为的一个js javascrpit:表示映射后的js的文件名和对象名 creator:表示Java对象的创建方式 |- new:表示直接使用new对象的方式创建 |- spring:表示对象使用spring方式创建 scope:表示映射的js所保存的属性范围 --> <create javascript="usersdao" creator="spring" scope="session"> <!-- <param>用来配置所映射的Java对象 name:表示配置的java对象的方式,与creator配合使用 |- class;如果为new的方式创建,则必须将 包.类名称 传入 |- beanName:如果为spring方式创建,则需要将对应的bean的id传入 value:依据选择的name进行填写 --> <param name="beanName" value="usersdaoimpl"></param> </create>
<!--<convert match="" converter="bean"></convert> --> </allow> </dwr> |
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base />
<title>regist.jsp</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/engine.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/util.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/interface/usersdao.js"></script> </head>
<body> <center> <html:form action="" method="post" focus="users.userid"> <table border="0"> <tr> <td>用户ID:</td> <td><html:text property="users.userid" /></td> </tr> <tr> <td>真实姓名:</td> <td><html:text property="users.realName" /></td> </tr> <tr> <td>密码:</td> <td><html:password property="users.password" /></td> </tr> <tr> <td>头像:</td> <td><html:file property="photo" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit value="注册"/></td> </tr> </table> </html:form> </center> </body> </html:html>
|
其中engine.js和util.js是dwr.jar中提供的支持js,依据Servlet配置映射到/dwr/下,而userdao.js是配置好的映射js,同样会在/dwr/,只不过所有的这种映射js在dwr中规定必须在interface虚拟目录下。
<script type="text/javascript"> function checkUserid(userid){ usersdao.checkUserid(userid,checkUseridCallback); } </script> |
<tr> <td>用户ID:</td> <td><html:text property="users.userid" onblur="checkUserid(this.value);" /></td> </tr> |
js方法中usersdao调用的checkUserid 方法为UsersDAO中的方法,必须与那个同名;
而标记为checkUserid的方法则只是页面上的方法,随意取名即可;
<script type="text/javascript"> function checkUserid(userid){ usersdao.checkUserid(userid,checkUseridCallback); } function checkUseridCallback(returnValue){ if(returnValue){ document.getElementById("checkMessage").innerHTML="<font color='red' size='-1'>用户名已存在</font>"; }else{ document.getElementById("checkMessage").innerHTML="<font color='blue' size='-1'>用户名可用</font>"; } } </script> |
<tr> <td>用户ID:</td> <td><html:text property="users.userid" onblur="checkUserid(this.value);" /><span id="checkMessage"></span></td> </tr> |
异常:
13:48:58,671 ERROR SpringCreator:75 - DWR can't find a spring config. See following info logs for solutions 13:48:58,687 ERROR SpringCreator:83 - Error java.lang.InstantiationException: DWR can't find a spring config. See the logs for solutions |
启动服务后出现错误,要求配置Spring监听,该监听用来给dwr提供spring配置文件的路径
在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml </param-value> </context-param>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>debug</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>/dwr/*</url-pattern> </servlet-mapping>
<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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|
OK