dwr实现用户管理demo

前端之家收集整理的这篇文章主要介绍了dwr实现用户管理demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

DWR:

Direct Web Remoting 是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。

demo:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>user-manage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
		<dependency>
			<groupId>org.directwebremoting</groupId>
			<artifactId>dwr</artifactId>
			<version>3.0.M1</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<configuration>
					<scanIntervalSeconds>200</scanIntervalSeconds>
					<webApp>
						<contextPath>/user</contextPath>
					</webApp>
					<connectors>
						<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
							<port>9080</port>
							<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
				</configuration>
				 <executions>  
                    <execution>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>run</goal>  
                        </goals>  
                    </execution>  
                </executions>  
			</plugin>
		</plugins>
	</build>
</project>


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<display-name>DWR (Direct Web Remoting)</display-name>
	<description>A Simple Demo DWR</description>
	<listener>
		<listener-class>
			org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class>
	</listener>
	<listener>
		<listener-class>
			org.directwebremoting.servlet.EfficientShutdownServletContextListener</listener-class>
	</listener>
	<!-- <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> 
		</listener> -->

	<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>

		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
</web-app>

dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>

  <allow>
    <create creator="new">
      <param name="class" value="com.demo.service.UserService"/>
    </create>
    <!-- 配置转换器 -->
	<convert converter="bean" match="com.demo.model.User"/>
  </allow>

</dwr>

UserService:

package com.demo.service;

import java.util.ArrayList;
import java.util.List;

import com.demo.model.User;

public class UserService {
	public static final String SUCCESS="success";
	public String add(User user){
		System.out.println(user);
		return SUCCESS;
	}
	public String deleted(int userId){
		System.out.println("userId:"+userId);
		return SUCCESS;
	}
	public String update(User user){
		System.out.println("update:"+user);
		return SUCCESS;
	}
	public List<User> list(){
		List<User> users=new ArrayList<User>();
		users.add(new User(1,"aa"));
		users.add(new User(2,"bb"));
		users.add(new User(3,"cc"));
		return users;
	}
}

list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>user update</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/UserService.js"></script>
<script type="text/javascript">
	
	UserService.list(callBack);
	
	function callBack(list){
		for(var i=0;i<list.length;i++){
			var user=list[i];
			alert(user.id+" "+user.name);
		}
	}
</script>
</head>
<body>

</body>
</html>

测试:

http://localhost:9080/user/list.jsp

猜你在找的Ajax相关文章