如下:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/UserDaoAjax.js"></script>
<title>注册</title>
</head>
<script type="text/javascript">
function show(boolean)
{
if(boolean)
{
alert('用户名已存在!');
}
}
function validate()
{
var name=registForm.name.value;
alert(name);
UserDaoAjax.exitUser(name,show);
}
</script>
<body onload="init()">
<div align="center">
<s:form id="registForm" action="register.do" method="post">
<p> </p>
<p> </p>
<table width="625" border="1" cellspacing="1" cellpadding="1">
<tr align="center">
<td colspan="1"><img src="/bookstore/images/dingdang.jpg"></td>
<td align="center" colspan="2">欢迎注册</td>
</tr>
<tr>
<td width="188" height="45"> </td>
<td width="200"> </td>
<td width="219"> </td>
</tr>
<tr>
<td align="right">*用户昵称:</td>
<td><s:textfield id="name" name="user.name" onchange="validate()"/></td>
<td> </td>
</tr>
<tr>
<td align="right">*密码:</td>
<td><s:password id="password" name="user.password" /></td>
<td> </td>
</tr>
<tr>
<td align="right">*请再次输入密码:</td>
<td><input type="password" id="password1" /></td>
<td> </td>
</tr>
<tr>
<td align="right">*邮箱:</td>
<td><s:textfield id="email" name="user.email" /></td>
<td> </td>
</tr>
<tr>
<td height="31" colspan="3" align="center"><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
</table>
</s:form>
</div>
</body>
</html>
dwr.xml文件如下:<dwr>
<allow>
<create creator="spring" javascript="UserDaoAjax">
<param name="beanName" value="userService" />
<include method="exitUser"/>
</create>
</allow>
</dwr>
applicationContext.xml文件部分如下(确定该文件配置没有问题): <bean id="userService" class="org.service.impl.UserService">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean> 就是输入昵称时,js里的 alert(name);已经输出,我设置了show_sql="true",但在后台却根本没显示查询数据库的 语句,大家帮我看看那个缓解鹅鹅鹅鹅鹅上还有纰漏?
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/UserDaoAjax.js"></script>
<title>注册</title>
</head>
<script type="text/javascript">
function show(boolean)
{
if(boolean)
{
alert('用户名已存在!');
}
}
function validate()
{
var name=registForm.name.value;
alert(name);
UserDaoAjax.exitUser(name,show);
}
</script>
<body onload="init()">
<div align="center">
<s:form id="registForm" action="register.do" method="post">
<p> </p>
<p> </p>
<table width="625" border="1" cellspacing="1" cellpadding="1">
<tr align="center">
<td colspan="1"><img src="/bookstore/images/dingdang.jpg"></td>
<td align="center" colspan="2">欢迎注册</td>
</tr>
<tr>
<td width="188" height="45"> </td>
<td width="200"> </td>
<td width="219"> </td>
</tr>
<tr>
<td align="right">*用户昵称:</td>
<td><s:textfield id="name" name="user.name" onchange="validate()"/></td>
<td> </td>
</tr>
<tr>
<td align="right">*密码:</td>
<td><s:password id="password" name="user.password" /></td>
<td> </td>
</tr>
<tr>
<td align="right">*请再次输入密码:</td>
<td><input type="password" id="password1" /></td>
<td> </td>
</tr>
<tr>
<td align="right">*邮箱:</td>
<td><s:textfield id="email" name="user.email" /></td>
<td> </td>
</tr>
<tr>
<td height="31" colspan="3" align="center"><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
</table>
</s:form>
</div>
</body>
</html>
dwr.xml文件如下:<dwr>
<allow>
<create creator="spring" javascript="UserDaoAjax">
<param name="beanName" value="userService" />
<include method="exitUser"/>
</create>
</allow>
</dwr>
applicationContext.xml文件部分如下(确定该文件配置没有问题): <bean id="userService" class="org.service.impl.UserService">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean> 就是输入昵称时,js里的 alert(name);已经输出,我设置了show_sql="true",但在后台却根本没显示查询数据库的 语句,大家帮我看看那个缓解鹅鹅鹅鹅鹅上还有纰漏?
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>encoding</filter-name>
<!--<filter-class>org.util.Encoding</filter-class>-->
<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>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<!-- filter-mapping确定过滤链的过滤顺序 -->
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 用于初始化Spring的Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dwr</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</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/user/home_page.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>struts.propertiesstruts.objectFactory=spring
struts.devMode=false
struts.action.extension=do,action
struts.i18n.reload=false
struts.ui.theme=simple
struts.locale=zh_CN
struts.serve.static.browserCache=false
struts.url.includeParams=none
applicationContext.xml文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 引入init.properties中属性 -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:init.properties</value>
</property>
</bean>
<!-- 配置数据源,连接池使用c3p0 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
</bean>
<!-- 配置Hibernate中的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource">
<ref local="dataSource" />
</property>
<!-- 配置Hibernate对应的映射资源 -->
<property name="mappingResources">
<list>
<value>org/model/Book.hbm.xml</value>
<value>org/model/BookComment.hbm.xml</value>
<value>org/model/BookScore.hbm.xml</value>
<value>org/model/Catalog.hbm.xml</value>
<value>org/model/Message.hbm.xml</value>
<value>org/model/Order.hbm.xml</value>
<value>org/model/OrderItem.hbm.xml</value>
<value>org/model/Shop.hbm.xml</value>
<value>org/model/ShopComment.hbm.xml</value>
<value>org/model/ShopScore.hbm.xml</value>
<value>org/model/User.hbm.xml</value>
</list>
</property>
<!-- 配置Hibernate的属性,包括断言,show_sql等 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
<prop key="hibernate.jdbc.fetch_size">
${hibernate.jdbc.fetch_size}
</prop>
<prop key="hibernate.jdbc.batch_size">
${hibernate.jdbc.batch_size}
</prop>
<prop key="hibernate.connection.release_mode">
${hibernate.connection.release_mode}
</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器bean -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 为事务管理器注入sessionFactory" -->
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 事务控制代理抽象定义 -->
<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!-- 为事务代理bean注入一个事物管理器 -->
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<!-- 定义事务传播属性 -->
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="release*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- BaseDao -->
<bean id="baseDao" class="org.dao.BaseDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- DAO -->
<bean id="userDao" class="org.dao.impl.UserDao" parent="baseDao"/>
<bean id="catalogDao" class="org.dao.impl.CatalogDao" parent="baseDao"/>
<bean id="bookDao" class="org.dao.impl.BookDao" parent="baseDao"/>
<bean id="commentDao" class="org.dao.impl.CommentDao" parent="baseDao"/>
<!-- Service -->
<bean id="userService" class="org.service.impl.UserService">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="catalogService" class="org.service.impl.CatalogService">
<property name="catalogDao">
<ref bean="catalogDao"/>
</property>
</bean>
<bean id="bookService" class="org.service.impl.BookService">
<property name="bookDao">
<ref bean="bookDao"/>
</property>
</bean>
<bean id="commentService" class="org.service.impl.CommentService">
<property name="commentDao">
<ref bean="commentDao"/>
</property>
</bean>
<!-- Action -->
<bean id="userAction" class="org.action.UserAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>
<bean id="bookAction" class="org.action.BookAction">
<property name="catalogService">
<ref bean="catalogService"/>
</property>
<property name="bookService">
<ref bean="bookService"/>
</property>
<property name="commentService">
<ref bean="commentService"/>
</property>
</bean>
</beans>
userDao.javapackage org.dao.impl;import java.util.*;
import org.model.User;
import org.dao.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class UserDao extends BaseDao implements IUserDao{ public boolean exitUser(String name){
Session sess=null;
String hql="from User u where u.name=?";
try
{
sess = getSession();
Iterator users=sess.createQuery(hql).setParameter(0, name).iterate();
if(users.hasNext())
return true;
else
return false;
}
catch (Exception e)
{
e.printStackTrace();
return true;
}
finally
{
sess.close();
}
}
}
如上,麻烦大家哈~红色的是我觉得可能会错的地方
这里写不对吧
boolean 是基础类型,不能做为你自定义的参数
修改一下试试function show(rtn)
{
if(rtn)
{
alert('用户名已存在!');
}
}
我引用的JS是用绝对路径 <script type="text/javascript" src="/dwr/engine.js"></script>
<script type="text/javascript" src="/dwr/util.js"></script>
<script type="text/javascript" src="/dwr/interface/UserDaoAjax.js"></script>另外,楼主可以访问http://127.0.0.1:8080/你的工程名/dwr/index.html
如果你的配置都没问题,可以先在这里测试一下你配置的方法
<script type="text/javascript" src="/工程名/dwr/engine.js"></script>
<script type="text/javascript" src="/工程名/dwr/util.js"></script>
<script type="text/javascript" src="/工程名/dwr/interface/UserDaoAjax.js"></script>