dwr, java, jsp, spring
1. Download the dwr.jar (also need commons-logging)
2. Add “<servlet>” and “<servlet-mapping>” into web.xml
3. Create a dwr.xml file. This file defines what classes DWR can create and remote for use by Javascript.
4. Testing url: http://localhost:8080/[YOUR-WEBAPP]/dwr/OK. Environment is setup. Do a simple example:
-----------------------------------------------------------
1. Inside my “hello.jsp” file’s header, insert
<script type=”text/javascript” src=”javascript/validator.js”></script>
<script type=”text/javascript” src=”/springapp/dwr/interface/DemoPercIncre.js”> </script>
<script type=”text/javascript” src=”/springapp/dwr/interface/PercentageIncre.js”> </script>
<script type=”text/javascript” src=’/springapp/dwr/engine.js’></script>
<script type=”text/javascript” src=’/springapp/dwr/util.js’></script>
<SCRIPT LANGUAGE=”JavaScript”>
function update() {
var percent = dwr.util.getValue(“percToIncre”);
PercentageIncre.increPrice(percent, function(data) {
dwr.util.setValue(“IncreProcessed”, data);
});
}
</SCRIPT>
2. Inside “Body”, create input box:
<p>Increase Price by Percent:
<input type=”text” id=”percToIncre” onChange=”digitvalidation(this, 1, 2,’You MUST enter 1 or 2 Integer Digits’,'Integer’);” />
<input value=”Excute” type=”button” onclick=”update()” />
<br />Response: <span id=”IncreProcessed“></span>
</p>
3. Create dwr.xml under WEB-INF, to link the javascript and the java-class.
<dwr>
<allow>
<create creator=”new” javascript=”PercentageIncre“>
<param name=”class” value=”springapp.web.PercentageIncre” />
</create>
</allow>
</dwr>
4. Then create the java-class: PercentageIncre.java
package springapp.web;
public class PercentageIncre {
public String increPrice(String perc) {
return “Hello, I pretend to increase the price by ” + perc + “% percent.”;
}
}
note: WEB-INF seems to be a special folder, my hello.jsp can’t find any js files fall into that folder by using “src=…”. I have to create a “javascript” folder parallel with WEB-INF, put js files in it, and in hello.jsp use “<script type=”text/javascript” src=”javascript/validator.js”></script>” to include that validator.js file.