SSH+AJAX 註冊使用者名稱唯一性驗證
阿新 • • 發佈:2019-02-10
不說廢話,上程式碼。
pojo
package com.pojo; public class Client { private Integer clientId; private String clientName; private String clientPass; public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName = clientName; } public String getClientPass() { return clientPass; } public void setClientPass(String clientPass) { this.clientPass = clientPass; } public Integer getClientId() { return clientId; } public void setClientId(Integer clientId) { this.clientId = clientId; } @Override public String toString() { return "Client [clientId=" + clientId + ", clientName=" + clientName + ", clientPass=" + clientPass + "]"; } }
<?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"> <hibernate-mapping package="com.pojo"> <class name="Client" table="client"> <id name="clientId" column="c_id" type="java.lang.Integer" > <generator class="increment"></generator> </id> <property name="clientName" column="c_name" type="java.lang.String" ></property> <property name="clientPass" column="c_pass" type="java.lang.String" ></property> </class> </hibernate-mapping>
Action
package com.action; import javax.servlet.http.HttpServletResponse; public class ClientAction extends ActionSupport{ private static final long serialVersionUID = 1L; private ClientService clientService; private Client client; public Client getClient() { return client; } public void setClient(Client client) { this.client = client; } public void setClientService(ClientService clientService) { this.clientService = clientService; } public String checkName() throws Exception{ String clientName = client.getClientName(); client.setClientName(clientName); Client checkName = clientService.checkName(clientName); HttpServletResponse response= ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); if(checkName != null){ //存在 response.getWriter().println("<font color='red'>使用者已經存在</font>"); }else{ response.getWriter().println("<font color='green'>使用者名稱可以使用</font>"); } return NONE; } }
package com.service;
import com.pojo.Client;
public interface ClientService {
public Client checkName(String clientName);
}
package com.service.impl;
import com.dao.ClientDao;
import com.pojo.Client;
import com.service.ClientService;
public class ClientServiceImpl implements ClientService {
private ClientDao clientDao;
public void setClientDao(ClientDao clientDao) {
this.clientDao = clientDao;
}
public Client checkName(String clientName) {
return clientDao.checkName(clientName);
}
}
package com.dao;
import com.pojo.Client;
public interface ClientDao {
public Client checkName(String clientName);
}
package com.dao.impl;
import java.util.List;
import com.dao.ClientDao;
import com.pojo.Client;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class ClientDaoImpl extends HibernateDaoSupport implements ClientDao {
public Client checkName(String clientName) {
String hql="from Client where clientName=?";
@SuppressWarnings("unchecked")
List<Client> list =this.getHibernateTemplate().find(hql,clientName);
if(list != null && list.size() > 0){
return list.get(0);
}
return null;
}
}
%@ page language="java" import="java.util.*" pageEncoding="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 'AddUser.jsp' starting page</title>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#clientName").blur(function(){
var clientName = $(this).val();
alert(clientName);
if(clientName==""){
$("#checkresult").html("使用者名稱不能為空");
}else{
// $.ajax方法實現
$.ajax({
url:"client_checkName.action",
type:"post",
data:{
"client.clientName":$("#clientName").val().toString()
},
dataType:"text",
async:true,
success:function(result){
$("#checkresult").html(result);
}
});
}
});
});
</script></head>
<body>
<div align="center">
please input New ClientName:<input type="text" name="client.clientName" id="clientName"><span id="checkresult"></span><br>
</div>
</body>
</html>
application配置檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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/ajax?useUnicode=true&characterEncoding=UTF-8"> </property> <property name="username" value="root"></property> <property name="password" value="mmy123"></property> </bean> <!--sessionFactory 配置檔案 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/pojo/client.hbm.xml</value> </list> </property> </bean> <!-- 配置事務的配置檔案 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager" /> <aop:config> <aop:pointcut expression="execution(public * com.service.*.*(..))" id="bussinessService"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager" > <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="add*" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" read-only="false" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 建立切面 --> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.service.*.*(..))"/> <!-- 將事物與切面組合 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <bean id="clientDao" class="com.dao.impl.ClientDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="clientService" class="com.service.impl.ClientServiceImpl"> <property name="clientDao" ref="clientDao"></property> </bean> <bean id="clientAction" class="com.action.ClientAction"> <property name="clientService" ref="clientService"></property> </bean> </beans>
這就是專案全部程式碼,記得匯入jar包,structs.xml檔案不需要配置。