一个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" οnclick="loginValidator()"/><input type="reset" value="reset"/></td></tr>
</table>
</s:form>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值