dwr框架入门程序

本文详细介绍了Java Web应用开发过程中使用DWR框架实现前后端交互的实践过程,包括实体类、业务逻辑类、服务接口及前端交互代码的实现。通过具体的例子展示了如何在Java Web应用中高效地进行前后端数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目目录:


Dept.java

package com.ideatc.dwr.domain;

/**
 * @ClassName: Dept 
 * @Description: 部门实体类 
 * @Company: IDEATC CO.,LTD 
 * @author dzy 
 * @date 2015-7-27 上午9:15:12 
 * @Version v1.0 
 */
public class Dept {
	private  Long id;
	private String name;
	public Dept() {
		super();
	}
	public Dept(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

}

DeptService.java

<span style="font-family: Arial, Helvetica, sans-serif;">package com.ideatc.dwr.service;</span>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ideatc.dwr.domain.Dept;

/**
 * @ClassName: DeptService 
 * @Description: 业务逻辑类
 * @Company: IDEATC CO.,LTD 
 * @author dzy
 * @date 2015-7-27 上午9:17:02 
 * @Version v1.0 
 */
@SuppressWarnings("all")
public class DeptService {
	
	public List findDept(){
		throw new RuntimeException("查找失败!");
	}
	
	public void deleteDept(Long id){
		System.out.println("Delete Dept " + id);
	}
	public List getDeptsForPo(){
		List depts = new ArrayList();
		depts.add(new Dept((long) 11,"教质部"));
		depts.add(new Dept((long) 21,"学术部"));
		depts.add(new Dept((long) 31,"就业部"));
		depts.add(new Dept((long) 41,"咨询部"));
		return depts;
	}
	
	public void saveDept(List<Dept> depts){
		System.out.println("Save Dept " + depts);
	}
	public List getDepts(){
		List depts = new ArrayList();
		Map map = new HashMap();
		map.put("id","01");
		map.put("name", "教质部");
		depts.add(map);
		map = new HashMap();
		map.put("id","02");
		map.put("name", "学术部");
		depts.add(map);
		map = new HashMap();
		map.put("id","03");
		map.put("name", "就业部");
		depts.add(map);
		map = new HashMap();
		map.put("id","04");
		map.put("name", "咨询部");
		depts.add(map);
		try{
			Thread.sleep(1000);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		return depts;
	}
	
}

HelloService.java


package com.ideatc.dwr.service;

/**
 * @ClassName: HelloServices 
 * @Description: TODO 
 * @Company: IDEATC CO.,LTD 
 * @author dzy 
 * @date 2015-7-27 上午9:27:12 
 * @Version v1.0 
 */

public class HelloService {
	
	public String sayHello(String name){
		System.out.println("Hello now !");
		return "Hello " + name + "!";
	}
}

LoginService.java

package com.ideatc.dwr.service;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

/**
 * @ClassName: LoginService 
 * @Description: TODO 
 * @Company: IDEATC CO.,LTD 
 * @author dzy
 * @date 2015-7-27 上午9:28:21 
 * @Version v1.0 
 */
public class LoginService {
	
	public void checkUserLogin(String userid,String pwd){
		WebContext webContext = WebContextFactory.get();
		webContext.getSession().setAttribute("userid", userid);
	}
	public String getLoginUser(){
		WebContext webContext = WebContextFactory.get();
		return (String) webContext.getSession().getAttribute("userid");
	}
}
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配置 -->
<dwr>
	<allow>
		<!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
		<create javascript="helloSrv" creator="new">
			<param name="class" value="com.ideatc.dwr.service.HelloService"></param>
		</create>
		<!-- 从Spring中获取Java对象 -->
		<create javascript="deptSrv" creator="spring">
			<!-- value的值是spring容器中的bean的id -->
			<param name="beanName" value="deptService"></param>
			<!-- 禁止执行 -->
			<exclude method="deleteDept"/>
		</create>
		<create javascript="loginSrv" creator="spring">
			<param name="beanName" value="loginSrv"></param>
		</create>
		<!-- 指定针对于特定对象的转换器 -->
		<convert match="com.ideatc.dwr.domain.*" converter="bean"></convert>
		<convert match="java.lang.Throwable" converter="bean">
			<param name="include" value="message"></param>
		</convert>
	</allow>
</dwr>

applicationContext.xml

<?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.0.xsd"> 
	
	<!-- 配置系统基础环境 -->
	<bean id="deptService" class="com.ideatc.dwr.service.DeptService"></bean>
	<bean id="loginSrv" class="com.ideatc.dwr.service.LoginService"></bean>
	
</beans>
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">
  <display-name></display-name>	
  
  <!-- spring的配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring/app*.xml</param-value>
  </context-param>
  
  <!-- spring的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置DWR前端控制器 -->
  <servlet>
  	<servlet-name>dwr</servlet-name>
  	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  	<!-- 指定配置文件 -->
  	<init-param>
  		<param-name>config</param-name>
  		<!-- 加载dwr.xml文件,该文件位于src下的config文件夹中 -->
  		<param-value>/WEB-INF/classes/dwr/dwr.xml</param-value>
  	</init-param>
  	<init-param>
  		<param-name>debug</param-name>
  		<param-value>true</param-value>
  	</init-param>
	<init-param>
		<param-name>crossDomainSessionSecurity</param-name>
		<param-value>false</param-value>
	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dwr</servlet-name>
  	<url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

dept.jsp

<%@ 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 'dept.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/helloSrv.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
  <script type="text/javascript">
  	function loadDept(){
  		//说明已经加载,不必重新加载 
  		if($('depts').options.length > 0){
  			return ;
  		}
  		DWRUtil.addOptions('depts',[{id:'',name:'正在下载...'}],'id','name');
		dwr.engine._execute("dwr",'deptSrv','getDepts',function(depts){
			DWRUtil.removeAllOptions('depts');
			DWRUtil.addOptions('depts',depts,'id','name');
		});
	}
	function loadDept2(){
		if($('depts2').options.length > 0){
			return ;
		}
		DWRUtil.addOptions('depts2',[{id:'',name:'正在下载...'}],'id','name');
		dwr.engine._execute("dwr",'deptSrv','getDeptsForPo',function(depts){
			DWRUtil.removeAllOptions('depts2');
			DWRUtil.addOptions('depts2',depts,'id','name');
		});
	}
	function saveDept(){
		//声明dept对象
		var dept = {
			id:$("deptid").value,
			name:$("deptname").value
		};
		dwr.engine._execute("dwr",'deptSrv','saveDept',[dept],function(){
			alert('保存成功!');
		});
	}
	function find(){
		dwr.engine._execute("dwr",'deptSrv','findDept',{
			callback:function(results){
				alert('查询成功!');
			},
			errorHandler:function(e){
				alert("查询失败:" + e);
			}
		});
	}
  </script>
  <body>
    <select id="depts" onclick="loadDept();"></select>
    <select id="depts2" onclick="loadDept2();"></select>
    <hr/>
    ID:<input id="deptid" type="text" size="8"/>
    NAME:<input id="deptname" type="text" size="8"/>
    <input value="保存部门" type="button" onclick="saveDept();"/>
    <input type="button" onclick="find();" value="查找"/>
  </body>
</html>
hello.jsp
<%@ 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>
    <title>My JSP 'hello.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <!-- 记得引入JS -->
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/helloSrv.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
  
  <script type="text/javascript">
  	function hello(){
  		//方法一
  		//返回处理后的结果信息
  		/* var fn = function(result){
  			$("msg").innerHTML = result;
  		}
  		helloService.sayHello($("name").value,fn); */
  		
  		//方法二
  		helloSrv.sayHello($("name").value,function(result){
  			$("msg").innerHTML = result;
  		}); 
  		
  		//方法三
  		//好处:不用导入上面的三个JS
  		//第一个参数:dwr访问路径,在web.xml中配置,如<url-pattern>/dwr/*</url-pattern>
  		//第二个参数:dwr与java服务器通信变量,在dwr.xml中声明
  		//第三个参数:服务器方法名
  		//第四个参数:页面请求参数,即服务器方法名的参数
  		//第五个参数:回调函数
//   		dwr.engine._execute("dwr","helloService","sayHello",$("name").value,fn);
  	}
  </script>
  <body>
    <div id="msg"></div>
	<input type="text" id="name" />
	<input type="button" value="Hello" onclick="hello();" />
  </body>
</html>

完整项目:http://download.youkuaiyun.com/detail/yong472727322/8946317






内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值