Struts2.318+Spring2.5.6+Hibernate3.x+DWR3
主要步骤如下:
在前一篇SSH整合基础上主要作以下修改:
1,dwr.jar and commons-logging.jar(Place it into the WEB-INF/lib directory of your web application.)
2,web.xml
增加如下:
<servlet><display-name>DWR Servlet</display-name><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></servlet-mapping>
3,WEB-INF下新建一个dwr.xml文件(注意:web.xml与dwr.xml是在同一个目录内),内容如下:
<dwr><allow><create creator="new" javascript="DWRUserAccess"><param name="class" value="com.ebooking.dwr.DWRUserAccess"></create><convert converter="bean" match="com.ebooking.model.EBooking"></convert></allow></dwr>
4,EBooking.java及DWRUserAccess.java:
===========DWRUserAccess.java 开发给前端用的后台服务===========
package com.ebooking.dwr;
import com.ebooking.dao.HelloWordDao;
import com.ebooking.dao.impl.HelloWordDaoImpl;
import com.ebooking.model.EBooking;
public class DWRUserAccess {
HelloWordDao helloWordDao = new HelloWordDaoImpl();
public boolean save(EBooking ebooking) {
System.out.println("in dwr method " + ebooking);
helloWordDao.list();
return true;
}
public boolean list() {
System.out.println("in dwr method ");
helloWordDao.list();
return true;
}
}
======================EBooking.java 为了从js转换成java bean所用========
package com.ebooking.model;
public class EBooking {
private Integer id;
private String name;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "name:"+this.name+" content:"+this.content;
}
}
5,前台调用 index.jsp==========
<script src="/ebooking/dwr/engine.js"></script><script src="/ebooking/dwr/util.js"></script><script src="/ebooking/dwr/interface/DWRUserAccess.js"></script><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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
This is my JSP page.
<a>doMethod</a>
<br><script type="text/javascript">
function doMethod()
{
alert("in here.....");
//调用方法:若公开的类是AAA,公开的AAA里的方法是bbb([参数]);
var stu = {name:"zhangsan",content:"zspassword"}; //这是json的表示方法
// AAA.bbb(stu,callBack);
//AAA.bbb([参数],callBack);//回调函数callBack()
DWRUserAccess.save(stu,callBack);
}
function callBack(data)//data是后台返回的值,名字自取,也可省略,因为JS允许
{
//如果callBack()没有写明返回值,可以通过argments[0]拿到
if(data){
alert("ok");
}else{
alert("not ok");
}
}
</script>
启动tomcat,运行:http://localhost:8080/ebooking/
成功后,在后台会看到如下内容:
log4j:WARN No appenders could be found for logger (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider).
log4j:WARN Please initialize the log4j system properly.
setSessionFactory sessionFactory:org.hibernate.impl.SessionFactoryImpl@668de5
in dwr method name:zhangsan content:zspassword
the list method in HelloWordDaoImpl
主要步骤如下:
在前一篇SSH整合基础上主要作以下修改:
1,dwr.jar and commons-logging.jar(Place it into the WEB-INF/lib directory of your web application.)
2,web.xml
增加如下:
<servlet><display-name>DWR Servlet</display-name><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></servlet-mapping>
3,WEB-INF下新建一个dwr.xml文件(注意:web.xml与dwr.xml是在同一个目录内),内容如下:
<dwr><allow><create creator="new" javascript="DWRUserAccess"><param name="class" value="com.ebooking.dwr.DWRUserAccess"></create><convert converter="bean" match="com.ebooking.model.EBooking"></convert></allow></dwr>
4,EBooking.java及DWRUserAccess.java:
===========DWRUserAccess.java 开发给前端用的后台服务===========
package com.ebooking.dwr;
import com.ebooking.dao.HelloWordDao;
import com.ebooking.dao.impl.HelloWordDaoImpl;
import com.ebooking.model.EBooking;
public class DWRUserAccess {
HelloWordDao helloWordDao = new HelloWordDaoImpl();
public boolean save(EBooking ebooking) {
System.out.println("in dwr method " + ebooking);
helloWordDao.list();
return true;
}
public boolean list() {
System.out.println("in dwr method ");
helloWordDao.list();
return true;
}
}
======================EBooking.java 为了从js转换成java bean所用========
package com.ebooking.model;
public class EBooking {
private Integer id;
private String name;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "name:"+this.name+" content:"+this.content;
}
}
5,前台调用 index.jsp==========
<script src="/ebooking/dwr/engine.js"></script><script src="/ebooking/dwr/util.js"></script><script src="/ebooking/dwr/interface/DWRUserAccess.js"></script><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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
This is my JSP page.
<a>doMethod</a>
<br><script type="text/javascript">
function doMethod()
{
alert("in here.....");
//调用方法:若公开的类是AAA,公开的AAA里的方法是bbb([参数]);
var stu = {name:"zhangsan",content:"zspassword"}; //这是json的表示方法
// AAA.bbb(stu,callBack);
//AAA.bbb([参数],callBack);//回调函数callBack()
DWRUserAccess.save(stu,callBack);
}
function callBack(data)//data是后台返回的值,名字自取,也可省略,因为JS允许
{
//如果callBack()没有写明返回值,可以通过argments[0]拿到
if(data){
alert("ok");
}else{
alert("not ok");
}
}
</script>
启动tomcat,运行:http://localhost:8080/ebooking/
成功后,在后台会看到如下内容:
log4j:WARN No appenders could be found for logger (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider).
log4j:WARN Please initialize the log4j system properly.
setSessionFactory sessionFactory:org.hibernate.impl.SessionFactoryImpl@668de5
in dwr method name:zhangsan content:zspassword
the list method in HelloWordDaoImpl