今天写了一个小功能!整合了一下DWR 嘿嘿。
下面开始讲了
第一步整合DWR 也就是DWR.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<!-- AJAX技术具体实现 -->
<allow>
<convert match="dk.Help2VO" converter="bean"></convert>
<create creator="new" javascript="Help2" scope="session">
<param name="class" value="dk.Help2"/>
<include method="readValue"/>
<include method="writeProperties"/>
<include method="readProperties"/>
</create>
</allow>
</dwr>
第二步注册dwr 在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">
<servlet>
<servlet-name>dwrInvoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwrInvoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第三步:导入DWr.jar,common-logging.jar 这两个 jar包
第四步就开始写Hlep2.java
package dk;
import java.io.*;
import java.util.*;
public class Help2 {
public String readProperties(){
Properties props = new Properties();
Help2VO vo2 = new Help2VO();
String allDate ="";
String filePath = "F:\\util\\feedback.properties";
try{
props.load(new FileInputStream(filePath));
allDate = props.getProperty("btValue1")+","+props.getProperty("btValue2")+","+props.getProperty("btValue3")+","+props.getProperty("btValue4");
/* vo2.setBtValue1(props.getProperty("btValue1"));
vo2.setBtValue2(props.getProperty("btValue2"));
vo2.setBtValue3(props.getProperty("btValue3"));
vo2.setBtValue4(props.getProperty("btValue4"));*/
}catch(Exception e){
e.printStackTrace();
}
return allDate;
}
public void writeProperties(String name,String value){
System.out.println("name===="+name);
System.out.println("value===="+value);
Properties props = new Properties();
String filePath = "F:\\util\\feedback.properties";
try{
InputStream fis = new FileInputStream(filePath);
props.load(fis);
fis.close();
OutputStream fos = new FileOutputStream(filePath);
props.setProperty(name, value);
props.store(fos,name);
fos.flush();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
/* public static void main(String[] args) {
Help2 p2 = new Help2();
System.out.println(p2.readProperties().getBtValue1());
System.out.println(p2.readProperties().getBtValue2());
System.out.println(p2.readProperties().getBtValue3());
System.out.println(p2.readProperties().getBtValue4());
}*/
}
这里的写法与上一篇的读写properties文件写法稍有不同,所以我这里使用的绝对路径,而不是在工程中了。
不过这写都是 问题不大的 稍微改变一下 输入输出流 就可以了!
hlep2VO.java
package dk;
public class Help2VO {
private String btValue1;
private String btValue2;
private String btValue3;
private String btValue4;
public String getBtValue1() {
return btValue1;
}
public void setBtValue1(String btValue1) {
this.btValue1 = btValue1;
}
public String getBtValue2() {
return btValue2;
}
public void setBtValue2(String btValue2) {
this.btValue2 = btValue2;
}
public String getBtValue3() {
return btValue3;
}
public void setBtValue3(String btValue3) {
this.btValue3 = btValue3;
}
public String getBtValue4() {
return btValue4;
}
public void setBtValue4(String btValue4) {
this.btValue4 = btValue4;
}
}
实际上页面上可以使用他 因为我返回的是String类型的 如果你返回的是VO类型的那么这里就需要了 在页面上需要使用JSON去获取vo中 的属性值
我这里开始是 试了一下 不过认为直接传String 然后切割 比较方便 所以就删除了
index.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 'index.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">
<script type='text/javascript' src='/DwrChangeButtonValue/dwr/interface/Help2.js'></script>
<script type='text/javascript' src='/DwrChangeButtonValue/dwr/engine.js'></script>
<script type='text/javascript' src='/DwrChangeButtonValue//dwr/util.js'></script>
<script>
function test(){
document.getElementById("div1").style.display='block';
var t1 = document.getElementById("t1").value;
}
function b(){
alert("成功");
document.getElementById("div1").style.display='none';
document.getElementById("t2").value=document.getElementById("t1").value;
var name = "btValue1";
var value = document.getElementById("t1").value;
requestHelp(name,value);
}
function requestHelp(name,value){
alert(name+"---->"+value);
Help2.writeProperties(name,value,callback2);
}
function callback2(data){
}
</script>
<script type="text/javascript">
function getBtValue(){
Help2.readProperties(callback);
}
function callback(data){
var data1 = data.split(",");
alert(data1);
for(var i=0;i<data1.length;i++){
document.getElementById('t2').value=data1[0];
}
}
getBtValue();
</script>
</HEAD>
<BODY>
<input type="button" value="12" id='t2'>
<a href="javascript:test()">编辑</a>
<div id="div1" style="display: none">
<input type="text" id="t1">
<input type="button" οnclick="b()" value="提交">
</div>
<!--
<input type="button" οnclick="getBtValue()" value="测试" >
-->
</html>
效果:
点击编辑按钮
向文本框中输入内容:
点击提交
点击确定
最后确定
修改成功
然后 无论你怎么刷新页面 也是没有关系的 这里就解决了 需要改变按钮的值 而要在被迫在数据库创建一个字段来储存了!
ok 工程我也上传上来!嘿嘿 供大家下载!