DWR集成Spring学习笔记

xiaoxiao2026-08-27  28

都知道spring很强大,而dwr 相对来说是ajax 一个很好的框架,下面就来看看如何用spring集成dwr 当然还是的是首先建立一个web工程 建立好之后 我们先写后台 UserManager .java package com.ajax.dwr; public class UserManager { public boolean getUser(String username) { System.out.println(username); if (username.equals("aaa")) { return true; } else { return false; } } } 写好之后再写web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <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"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>dwr</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/SpringDWR/*</url-pattern> </servlet-mapping> <!-- 这种也可以 --> <!-- <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>--> <!-- 监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> </web-app> 然后我们在写一个dwr.xml <?xml version="1.0" encoding="UTF-8"?> <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"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>dwr</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/SpringDWR/*</url-pattern> </servlet-mapping> <!-- 这种也可以 --> <!-- <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>--> <!-- 监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> </web-app> 然后我们看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.5.xsd"> <bean id="SpringManager" class="com.ajax.dwr.UserManager"> </bean> </beans> 配置文件写完了,然后我们在写html页面 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <!-- 引用样式 --> <script type="text/javascript" src="SpringDWR/engine.js"></script> <script type="text/javascript" src="SpringDWR/util.js"></script> <script type="text/javascript" src="SpringDWR/interface/aaa.js"></script> <script type="text/javascript"> function check1() { var aa=document.getElementById("name1").value; if(aa.length==0) { alert("用户名不能为空"); return false; } return true; } function checkRepeat() { if(check1()) { var name=dwr.util.getValue("name"); //类里面的方法 aaa.getUser(name,function(data) { //data==true if(data) { alert("用户名已经存在"); } else { alert("该用户名可以使用"); } }); } } </script> </head> <body> 用户名: <input type="text" name="name" id="name1"> <input type="button" value="检查用户名是否重复" οnclick="checkRepeat()"> </body> </html>
转载请注明原文地址: https://www.6miu.com/read-5051945.html

最新回复(0)