一个DWR的简单例子
java文件:
package com.demo.action.user;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.demo.pojo.user.User;
import com.demo.service.user.UserService;
/**
* @author XiongShunHong
* @date 2009/4/28
*/
@Controller("loginValidate")
public class LoginValidate {
@Autowired
private UserService userService;
public boolean loginValidate(String name,String password){
boolean mark=false;
List<User> list=userService.userList();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
if(user.getName().equals(name) && user.getPassword().equals(password)){
mark=true;
return mark;
}
else{
continue;
}
}
return mark;
}
public void check(){
System.out.println(123);
}
}
-------------------------------
dwr.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="loginValidate" creator="spring">
<param name="beanName" value="loginValidate"></param>
</create>
</allow>
</dwr>
----------------------------------
web.xml配置
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
------------------------------
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
welcome to use my system if you haven't account please click here <a href="<%=request.getContextPath() %>/getCompaneyList.action">register</a>
<head>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'> </script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'> </script>
<script type="text/javascript" src='<%=request.getContextPath()%>/dwr/interface/loginValidate.js'></script>
<script type="text/javascript" src='<%=request.getContextPath()%>/dwr/interface/dwrCheck.js'></script>
<script type="text/javascript">
function loginValidator() {
var userName = dwr.util.getValue("user.name");
var password = dwr.util.getValue("user.password");
var mark=false;
loginValidate.loginValidate(userName,password,function(mark){
if(mark){
document.forms[0].submit();
}
else{
dwr.util.setValue("error","userName or password error, please input again!");
}
});
}
</script>
</head>
<body>
<s:form action="/login.action" target="right">
<th><font color="red" id="error"></font></th>
<table>
<tr><td><s:textfield name="user.name" label="UserName" title="please input your userName"></s:textfield></td></tr>
<tr><td><s:password name="user.password" label="Password" title="please input your password"></s:password></td></tr>
<tr><td colspan="1"></td><td><input type="button" value="submit" onclick="loginValidator()"/><input type="reset" value="reset"/></td></tr>
</table>
</s:form>
</body>
</html>