- 浏览: 27030 次
- 性别:
- 来自: 深圳
最新评论
[color=black][color=white][color=white][color=white]当 Web 容器 加载 dwr时 时常见到 有如下警告 :
WARN - Failed to start annotations
java.lang.IllegalArgumentException: DefaultContainer can't find a classes
是 dwr2.0 加入了 JDK5的注释 。DwrServlet 初始化的时候会去检查注释的类,找不到就报出警告了。 可以如下解决 :在 加载 dwr 的 servlet 配置 下 加上 红色字体的配置
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
[color=red] <init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
Dwr 与 servlet
有2个java 类 需要用在 dwr 中 WebContext 和 WebContextFactory
这 2个 类给与 访问标准 HttpServlet 对象的入口 这些对象是 :
HttpServletRequest
HttpServletResponse
HttpSession
ServletContext
public class DwrUtil {
private WebContext wc = WebContextFactory.get();
public HttpServletRequest gethr(){
HttpServletRequest hr = wc.getHttpServletRequest();
if(hr!=null){
return hr;
}
return hr;
}
public HttpSession geths(){
HttpSession hs = wc.getSession();
if(hs!=null){
return hs;
}
return hs;
}
}
Dwr 与 spring
1.dwr里使用spring 数据源
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.directwebremoting.servlet.DwrServlet;
public class DwrUtil {
private WebContext wc = WebContextFactory.get();
public HttpServletRequest getReuqest(){
HttpServletRequest req = wc.getHttpServletRequest();
if(req!=null){
return req;
}
return req;
}
public HttpSession getSession(){
HttpSession session = wc.getSession();
if(session!=null){
return session;
}
return session;
}
session = new DwrUtil().getSession();
//采用spring数据源
WebApplicationContext context = (WebApplicationContext)session.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
DataSource ds = (DataSource)context.getBean("dataSource");
con = ds.getConnection();
String sql = "select [password] from atmuser where [name] = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,name);
rs = pstmt.executeQuery();
if(rs.next()){
anw = "<font color=red>该用户名已被其他用户注册</font>";
}else{
anw = "<font color=green>您可以使用该用户名</font>";
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
session.invalidate();
} catch (SQLException e) {
e.printStackTrace();
}
}
Dwr.xml :
<dwr>
<allow>
<create creator="new" javascript="hello">
<param name="class" value="com.dwrutil.CheckName"/>
</create>
</allow>
</dwr>
<script type='text/javascript' src='/strutsspring/lixing/interface/hello.js'></script>
<script type='text/javascript' src='/strutsspring/lixing/engine.js'></script>
<script type='text/javascript' src='/strutsspring/lixing/util.js'></script>
<script type="text/javascript">
function check(name){
hello.getRs(name,mm);
}
function mm(data){
document.getElementById("hi").innerHTML=data;
}
</script>
dwr在servlet配置文件的配置:
<servlet>
<servlet-name>dwr</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>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/lixing/*</url-pattern>
</servlet-mapping>
2. 使用 spring Ioc 与 dwr 整合 和 spring bean 的继承
环境 :
1. 确认 dwr 是 2.0的版本
2.确认 Spring 的 Bean 在 dwr 外面运行良好
3. 配置 Spring 与 dwr 一起工作
配置如下 :
Dwr.xml -------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="log">
<param name="beanName" value="checklog"/>
</create>
</allow>
</dwr>
Dwrutil.xml—
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="checklog" class="com.anddy.dwr.CheckLogin">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
</beans>
CheckLogin 类 继承JdbcDaoSupport 故 必须在 该类的bean 配置注入dataSource
采用spring 的 JdbcDaoSupport 类 来 进行 数据操作
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class CheckLogin extends JdbcDaoSupport{
private String sql = "select count(*) from users where u_name = ? and u_pwd = ?";
public String getUser(String name,String pwd){
int i = getJdbcTemplate().queryForInt(sql, new Object[]{name,pwd});
if(i>0){
return "";
}else{
return "<font color=red>用户名或密码错误</font>";
}
}
}
Mani.xml------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configbean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/dbcon.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${Condriver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${uid}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
</bean>
配置 hibernate sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingLocations">
<list>
<value>classpath:/com/anddy/user/dao/pojo/WebUser.hbm.xml</value>
<value>classpath:/com/anddy/user/dao/pojo/Myuser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
配置hibernate 事物管理器
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
配置 事物代理工厂bean
<bean id="transactionProxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> 使用 abstract 属性 (父 bean)
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
User.xml--------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userDao" class="com.anddy.user.dao.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userSer" class="com.anddy.user.web.biz.UserSerImpl">
<property name="userdao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="userSerProxy" parent="transactionProxyBean"> 使用parent 属性(子bean) 将 父bean 的配置 继承 很大程度 减少代码的复写
<property name="target">
<ref bean="userSer"/>
</property>
</bean>
<bean name="/reg" class="com.anddy.user.web.action.ReguserAction">
<property name="userSer">
<ref bean="userSerProxy"/>
</property>
</bean>
</beans>
Import.xml (spring 的总配置文件)---
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="classpath:/com/anddy/cfg/beans/main.xml"/>
<import resource="classpath:/com/anddy/cfg/beans/user.xml"/>
<import resource="classpath:/com/anddy/cfg/beans/dwrutil.xml"/>
</beans>
Web.xml ---------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.anddy.util.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/import.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>springcontext</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/classes/com/anddy/cfg/struts/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr</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>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/classes/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/mydwr/*</url-pattern>
</servlet-mapping>
</web-app>[/color][/color][/color][/color]
分页
1. 页面加载
<script type="text/javascript">
var start = 1;
function showcomment(){
var str = document.getElementById('hotelid').value;
HotelCommentAction.listCommentByHotel(str,start,show);
}
function show(data){//回调函数 设置内容显示的 div层 data即查出来的数据
document.getElementById("comment").innerHTML=data;
}
window.onload = showcomment;
function page(id){
start=id;
showcomment();
}
</script>
2.action内处理分页菜单 与 页面内容的组装
public String listCommentByHotel(long userId,int j)throws DataAccessException{// j 指页面传来的页数
if(Long.valueOf(userId) == null || Long.valueOf(userId) <= 0 || String.valueOf(userId)== ""){
return "<div id=\"ErrorDiv\" align='center'>对不起!该酒店尚无用户评论。</div>";
}
if(user==null){
user = new User();
}
int length = 5;//指定分页显示的条数
int offer=0;//从数据库查询记录的起点
offer = (j-1)*length;//根据 j 计算每次查询的起点
int totalRow = Long.valueOf(userservice.count(user)).intValue();//查询出总记录数
userList = userservice.list(user,offer,length); //查询出每页的列表
if(userList.size() == 0){ //若列表长度是0 则 return 提示
return "<div id=\"ErrorDiv\" align='center'>对不起!无用户评论。</div>";
}
StringBuffer bf = new StringBuffer();
for(int i = 0;i < userList.size();i++){ //循环组装静态标签 和 所查询出来的值
user = (User)userList.get(i);
………bf.append()结合 div 等静态标签组装user的值
}
bf.append("</br>");
//分页的组装
int totalpage = totalRow%5==0?totalRow/5:totalRow/5+1;
bf.append("<div align='right'>");
bf.append("当前 ").append(j).append("/").append(totalpage);
bf.append(" 页 共").append(totalRow).append("条 ");
bf.append("<a style=\"cursor:hand\" onclick=\"page(1);\" alt=\"首 页\">首页</a> ");
if(j==1){
bf.append("上一页 ");
}else{
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(j==1?j:j-1).append(");\" alt=\"上一页\">上一页</a> ");
}
if(j==totalpage){
bf.append("下一页 ");
}else{
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(j==totalpage?totalpage:j+1).append(")\" alt=\"下一页\">下一页</a> ");
}
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(totalpage).append(")\" alt=\"尾 页\">尾页</a>");
bf.append("</div>");
return bf.toString();
}[/color]
WARN - Failed to start annotations
java.lang.IllegalArgumentException: DefaultContainer can't find a classes
是 dwr2.0 加入了 JDK5的注释 。DwrServlet 初始化的时候会去检查注释的类,找不到就报出警告了。 可以如下解决 :在 加载 dwr 的 servlet 配置 下 加上 红色字体的配置
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
[color=red] <init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
Dwr 与 servlet
有2个java 类 需要用在 dwr 中 WebContext 和 WebContextFactory
这 2个 类给与 访问标准 HttpServlet 对象的入口 这些对象是 :
HttpServletRequest
HttpServletResponse
HttpSession
ServletContext
public class DwrUtil {
private WebContext wc = WebContextFactory.get();
public HttpServletRequest gethr(){
HttpServletRequest hr = wc.getHttpServletRequest();
if(hr!=null){
return hr;
}
return hr;
}
public HttpSession geths(){
HttpSession hs = wc.getSession();
if(hs!=null){
return hs;
}
return hs;
}
}
Dwr 与 spring
1.dwr里使用spring 数据源
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.directwebremoting.servlet.DwrServlet;
public class DwrUtil {
private WebContext wc = WebContextFactory.get();
public HttpServletRequest getReuqest(){
HttpServletRequest req = wc.getHttpServletRequest();
if(req!=null){
return req;
}
return req;
}
public HttpSession getSession(){
HttpSession session = wc.getSession();
if(session!=null){
return session;
}
return session;
}
session = new DwrUtil().getSession();
//采用spring数据源
WebApplicationContext context = (WebApplicationContext)session.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
DataSource ds = (DataSource)context.getBean("dataSource");
con = ds.getConnection();
String sql = "select [password] from atmuser where [name] = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,name);
rs = pstmt.executeQuery();
if(rs.next()){
anw = "<font color=red>该用户名已被其他用户注册</font>";
}else{
anw = "<font color=green>您可以使用该用户名</font>";
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
session.invalidate();
} catch (SQLException e) {
e.printStackTrace();
}
}
Dwr.xml :
<dwr>
<allow>
<create creator="new" javascript="hello">
<param name="class" value="com.dwrutil.CheckName"/>
</create>
</allow>
</dwr>
<script type='text/javascript' src='/strutsspring/lixing/interface/hello.js'></script>
<script type='text/javascript' src='/strutsspring/lixing/engine.js'></script>
<script type='text/javascript' src='/strutsspring/lixing/util.js'></script>
<script type="text/javascript">
function check(name){
hello.getRs(name,mm);
}
function mm(data){
document.getElementById("hi").innerHTML=data;
}
</script>
dwr在servlet配置文件的配置:
<servlet>
<servlet-name>dwr</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>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/lixing/*</url-pattern>
</servlet-mapping>
2. 使用 spring Ioc 与 dwr 整合 和 spring bean 的继承
环境 :
1. 确认 dwr 是 2.0的版本
2.确认 Spring 的 Bean 在 dwr 外面运行良好
3. 配置 Spring 与 dwr 一起工作
配置如下 :
Dwr.xml -------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="log">
<param name="beanName" value="checklog"/>
</create>
</allow>
</dwr>
Dwrutil.xml—
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="checklog" class="com.anddy.dwr.CheckLogin">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
</beans>
CheckLogin 类 继承JdbcDaoSupport 故 必须在 该类的bean 配置注入dataSource
采用spring 的 JdbcDaoSupport 类 来 进行 数据操作
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class CheckLogin extends JdbcDaoSupport{
private String sql = "select count(*) from users where u_name = ? and u_pwd = ?";
public String getUser(String name,String pwd){
int i = getJdbcTemplate().queryForInt(sql, new Object[]{name,pwd});
if(i>0){
return "";
}else{
return "<font color=red>用户名或密码错误</font>";
}
}
}
Mani.xml------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configbean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/dbcon.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${Condriver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${uid}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
</bean>
配置 hibernate sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingLocations">
<list>
<value>classpath:/com/anddy/user/dao/pojo/WebUser.hbm.xml</value>
<value>classpath:/com/anddy/user/dao/pojo/Myuser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
配置hibernate 事物管理器
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
配置 事物代理工厂bean
<bean id="transactionProxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> 使用 abstract 属性 (父 bean)
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
User.xml--------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userDao" class="com.anddy.user.dao.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userSer" class="com.anddy.user.web.biz.UserSerImpl">
<property name="userdao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="userSerProxy" parent="transactionProxyBean"> 使用parent 属性(子bean) 将 父bean 的配置 继承 很大程度 减少代码的复写
<property name="target">
<ref bean="userSer"/>
</property>
</bean>
<bean name="/reg" class="com.anddy.user.web.action.ReguserAction">
<property name="userSer">
<ref bean="userSerProxy"/>
</property>
</bean>
</beans>
Import.xml (spring 的总配置文件)---
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="classpath:/com/anddy/cfg/beans/main.xml"/>
<import resource="classpath:/com/anddy/cfg/beans/user.xml"/>
<import resource="classpath:/com/anddy/cfg/beans/dwrutil.xml"/>
</beans>
Web.xml ---------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.anddy.util.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/import.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>springcontext</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/classes/com/anddy/cfg/struts/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr</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>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/classes/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/mydwr/*</url-pattern>
</servlet-mapping>
</web-app>[/color][/color][/color][/color]
分页
1. 页面加载
<script type="text/javascript">
var start = 1;
function showcomment(){
var str = document.getElementById('hotelid').value;
HotelCommentAction.listCommentByHotel(str,start,show);
}
function show(data){//回调函数 设置内容显示的 div层 data即查出来的数据
document.getElementById("comment").innerHTML=data;
}
window.onload = showcomment;
function page(id){
start=id;
showcomment();
}
</script>
2.action内处理分页菜单 与 页面内容的组装
public String listCommentByHotel(long userId,int j)throws DataAccessException{// j 指页面传来的页数
if(Long.valueOf(userId) == null || Long.valueOf(userId) <= 0 || String.valueOf(userId)== ""){
return "<div id=\"ErrorDiv\" align='center'>对不起!该酒店尚无用户评论。</div>";
}
if(user==null){
user = new User();
}
int length = 5;//指定分页显示的条数
int offer=0;//从数据库查询记录的起点
offer = (j-1)*length;//根据 j 计算每次查询的起点
int totalRow = Long.valueOf(userservice.count(user)).intValue();//查询出总记录数
userList = userservice.list(user,offer,length); //查询出每页的列表
if(userList.size() == 0){ //若列表长度是0 则 return 提示
return "<div id=\"ErrorDiv\" align='center'>对不起!无用户评论。</div>";
}
StringBuffer bf = new StringBuffer();
for(int i = 0;i < userList.size();i++){ //循环组装静态标签 和 所查询出来的值
user = (User)userList.get(i);
………bf.append()结合 div 等静态标签组装user的值
}
bf.append("</br>");
//分页的组装
int totalpage = totalRow%5==0?totalRow/5:totalRow/5+1;
bf.append("<div align='right'>");
bf.append("当前 ").append(j).append("/").append(totalpage);
bf.append(" 页 共").append(totalRow).append("条 ");
bf.append("<a style=\"cursor:hand\" onclick=\"page(1);\" alt=\"首 页\">首页</a> ");
if(j==1){
bf.append("上一页 ");
}else{
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(j==1?j:j-1).append(");\" alt=\"上一页\">上一页</a> ");
}
if(j==totalpage){
bf.append("下一页 ");
}else{
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(j==totalpage?totalpage:j+1).append(")\" alt=\"下一页\">下一页</a> ");
}
bf.append("<a style=\"cursor:hand\" onclick=\"page(").append(totalpage).append(")\" alt=\"尾 页\">尾页</a>");
bf.append("</div>");
return bf.toString();
}[/color]
相关推荐
1、 导入dwr.jar包 2、 在web.xml中配置dwr,如下: <!-- 配置DWR --> <servlet-name>dwr-invoker org.directwebremoting.servlet.DwrServlet <init-param> <param-name>debug</param-name> ...
DWR (Direct Web Remoting) 是一个开源Java库,它允许Web应用程序在浏览器和服务器之间进行实时通信,无需使用插件或复杂的JavaScript框架。DWR简化了AJAX(Asynchronous JavaScript and XML)开发,使开发者可以像...
Direct Web Remoting (DWR) 是一种开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,从而实现动态Web应用程序。DWR的主要功能是通过AJAX技术在客户端和服务器之间进行实时通信,使得用户界面...
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在Web浏览器中直接调用Java方法,从而实现实时的、双向的通信。在“DWR3实现服务器端向客户端精确推送消息”这一主题中,我们将深入探讨如何利用DWR3进行...
DWR.xml配置文件说明书 1、 建立dwr.xml 配置文件 任何一个dwr.xml的文件都需要包含DWR DOCTYPE的声明行,格式如下: <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" ...
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在客户端与服务器端进行直接的交互,从而实现在Web应用程序中的Ajax功能。DWR的主要目标是简化前后端的数据交换,提高用户体验,使得Web应用能够像桌面...
DWR(Direct Web Remoting)是一种JavaScript库,它允许JavaScript代码在客户端与服务器端进行直接交互,从而实现实时Web应用程序。在这个特定的场景中,我们利用DWR来实现一个新消息的定时提醒功能,这在许多Web...
这个压缩包包含了DWR的核心组件`engine.js`和辅助库`util.js`,它们是实现DWR功能的关键。 `engine.js`是DWR的核心脚本,它实现了JavaScript和Java之间的远程调用(Remote Procedure Calls, RPC)。这个文件处理了...
DWR (Direct Web Remoting) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,实现动态的Web应用。在这个场景中,我们将讨论如何使用DWR来实现简单的文件上传功能。 文件上传是Web...
DWR(Direct Web Remoting)是一种JavaScript到Java的远程调用技术,允许Web应用程序在客户端与服务器之间进行实时通信,而无需刷新整个页面。DWR的出现极大地提升了Web应用的用户体验,因为它允许开发者在前端与...
Direct Web Remoting (DWR) 是一个开源Java库,它允许在浏览器和服务器之间进行安全、高效的异步通信,即所谓的“反向AJAX”或“Comet”技术。DWR使得JavaScript能够调用服务器端的Java方法,就像它们是本地函数一样...
DWR (Direct Web Remoting) 是一种开源Java技术,它允许Web应用程序在浏览器和服务器之间进行实时、双向通信,使得JavaScript可以直接调用服务器端的Java方法,极大地简化了客户端和服务器端的数据交换。本笔记将...
**DWR(Direct Web Remoting)**是一种Java技术,它允许Web应用程序在客户端与服务器之间进行实时通信,而无需刷新整个页面。通过DWR,我们可以使用JavaScript直接调用服务器端的Java方法,实现Ajax(Asynchronous ...
DWR (Direct Web Remoting) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,实现动态的Web应用。DWR简化了AJAX(Asynchronous JavaScript and XML)的开发,使得开发者可以像调用...
SpringBoot整合Direct Web Remoting (DWR)是一个常见的技术实践,它允许JavaScript在浏览器端直接调用服务器端的Java方法,极大地增强了Web应用的交互性。在这个过程中,我们通常会结合使用FreeMarker或JSP作为视...
SpringMVC 和 DWR(Direct Web Remoting)是两种在Web开发中用于增强交互性的技术。SpringMVC作为Spring框架的一部分,主要用于构建后端服务,而DWR则是一种JavaScript库,允许JavaScript与Java服务器进行实时通信,...
这个压缩包包含了DWR的三个主要版本:DWR1.0、DWR2.0和DWR3.0的jar包,这些jar包是运行DWR应用的核心组件。 DWR1.0: DWR1.0是DWR项目的早期版本,主要目标是简化Web应用中的异步通信。在这个版本中,DWR提供了一个...
这个压缩包包含了DWR的三个关键组件:DWR3.0.jar,DWR的实例war文件,以及DWR2.0的中文说明文档。以下是关于这些组件的详细知识: 1. **DWR3.0.jar**: DWR3.0.jar是DWR框架的核心库,包含所有必需的类和接口,...
在本教程中,我们将探讨如何利用Direct Web Remoting (DWR) 和Spring框架来实现这样的功能。 DWR是一个开源Java库,它允许Web应用程序在浏览器和服务器之间进行实时的、异步的通信,类似于Ajax的功能,但更加强大。...