SpringMVC integrates DWR

Source: Internet
Author: User

Version:
Spring3.1.2
Dwr3.0.M1
Web. xml configuration information
[Html
<! -- Spring mvc -->
<Servlet>
<Servlet-name> dispatcherServlet </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value> classpath: spring-mvc-servlet.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
 
<Servlet-mapping>
<Servlet-name> dispatcherServlet </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>

<Servlet-mapping>
<Servlet-name> dispatcherServlet </servlet-name>
<Url-pattern>/dwr/* </url-pattern>
</Servlet-mapping>

Configuration in spring-mvc-servlet.xml
[Html]
<Dwr: configuration>
<Dwr: convert type = "bean" class = "org. mvn. dwr. model. User"> </dwr: convert>
</Dwr: configuration>
<Dwr: url-mapping/>
<Dwr: controller id = "dwrController" debug = "true"/>

<Bean class = "org. mvn. dwr. dao. impl. UserDaoImpl" id = "userDaoImpl">
</Bean>

<Bean class = "org. mvn. dwr. service. impl. UserServiceImpl" id = "userServiceImpl">
<Property name = "userDao" ref = "userDaoImpl"> </property>
<Dwr: remote javascript = "dwrUserService">
<Dwr: include method = "checkUsernameIsExists"/>
<Dwr: include method = "getUsername"/>
<Dwr: include method = "findUserById"/>
<Dwr: include method = "listAll"/>
</Dwr: remote>
</Bean>

User. java
[Java]
Package org. mvn. dwr. model;
 
 
Public class User {
Private long id;
Private String username;
Private String birthday;
Private String address;

Public User (){
}

Public User (long id, String username, String birthday, String address ){
Super ();
This. id = id;
This. username = username;
This. birthday = birthday;
This. address = address;
}

Public long getId (){
Return id;
}
 
Public void setId (long id ){
This. id = id;
}
Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
Public String getBirthday (){
Return birthday;
}
Public void setBirthday (String birthday ){
This. birthday = birthday;
}
Public String getAddress (){
Return address;
}
Public void setAddress (String address ){
This. address = address;
}



}

UserDao. java
[Java]
Package org. mvn. dwr. dao;
 
Import java. util. List;
 
Import org. mvn. dwr. model. User;
 
Public interface UserDao {

Boolean checkUsernameIsExists (String username );

String getUsername (long id );

User findUserById (long id );

List <User> listAll ();

}

UserDaoImpl. java
[Java]
Package org. mvn. dwr. dao. impl;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import org. mvn. dwr. dao. UserDao;
Import org. mvn. dwr. model. User;
 
Public class UserDaoImpl implements UserDao {
 
@ Override
Public boolean checkUsernameIsExists (String username ){
Return true;
}
 
@ Override
Public String getUsername (long id ){
Return "dwrservice ";
}
 
@ Override
Public User findUserById (long id ){
Return new User (1, "admin", "1987-02-03", "Xi'an, Shaanxi ");
}
 
Public List <User> listAll (){
List <User> lists = new ArrayList <User> ();
For (int I = 1; I <= 100; I ++ ){
Lists. add (new User (I, "admin" + I, "1987-02-03", "Xi'an, Shaanxi" + I ));
}
Return lists;
}
}

UserService. java
[Java]
Package org. mvn. dwr. service;
 
Import java. util. List;
 
Import org. mvn. dwr. model. User;
 
Public interface UserService {

Boolean checkUsernameIsExists (String username );

String getUsername (long id );

User findUserById (long id );

List <User> listAll ();
}

UserServiceImpl. java
[Java]
Package org. mvn. dwr. service. impl;
 
Import java. util. List;
 
Import org. mvn. dwr. dao. UserDao;
Import org. mvn. dwr. model. User;
Import org. mvn. dwr. service. UserService;
 
Public class UserServiceImpl implements UserService {

Private UserDao userDao;

Public void setUserDao (UserDao userDao ){
This. userDao = userDao;
}
 
@ Override
Public boolean checkUsernameIsExists (String username ){
Return userDao. checkUsernameIsExists (username );
}
 
@ Override
Public String getUsername (long id ){
Return userDao. getUsername (id );
}
 
@ Override
Public User findUserById (long id ){
Return userDao. findUserById (id );
}
 
@ Override
Public List <User> listAll (){
Return userDao. listAll ();
}
 
}

Add in spring-mvc-servlet.xml
Xmlns: dwr = "http://www.directwebremoting.org/schema/spring-dwr"
Http://www.directwebremoting.org/schema/spring-dwr
Http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
[Html]
<Dwr: configuration>
<Dwr: convert type = "bean" class = "org. mvn. dwr. model. User"> </dwr: convert>
</Dwr: configuration>
<Dwr: url-mapping/>
<Dwr: controller id = "dwrController" debug = "true"/>

<Bean class = "org. mvn. dwr. dao. impl. UserDaoImpl" id = "userDaoImpl">
</Bean>

<Bean class = "org. mvn. dwr. service. impl. UserServiceImpl" id = "userServiceImpl">
<Property name = "userDao" ref = "userDaoImpl"> </property>
<Dwr: remote javascript = "dwrUserService">
<Dwr: include method = "checkUsernameIsExists"/>
<Dwr: include method = "getUsername"/>
<Dwr: include method = "findUserById"/>
<Dwr: include method = "listAll"/>
</Dwr: remote>
</Bean>

<Dwr: configuration>
<Dwr: convert type = "bean" class = "org. mvn. dwr. model. User"> </dwr: convert>
</Dwr: configuration>
Converts users.
<Dwr: url-mapping/>
<Dwr: controller id = "dwrController" debug = "true"/>
Two cannot be less
<Dwr: remote javascript = "dwrUserService">
<Dwr: include method = "checkUsernameIsExists"/>
<Dwr: include method = "getUsername"/>
<Dwr: include method = "findUserById"/>
<Dwr: include method = "listAll"/>
</Dwr: remote>
DwrUserService specifies the name of the js file generated by dwr.
Dwr: include indicates the methods that need to be enabled for remote calls.
Do not forget to configure
[Html]
<Bean id = "simpleUrlHandlerMapping" class = "org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">
<Property name = "alwaysUseFullPath" value = "true"> </property>
<Property name = "mappings">
<Props>
<Prop key = "/dwr/*"> dwrController </prop>
<Prop key = "/interface/**"> dwrController </prop>
</Props>
</Property>
</Bean>

There is a strange problem here
The above dao and service will report strange exceptions during annotation injection. I don't know why, so I don't have to worry about it now. I changed countless dwr jar packages throughout the integration process. It can be said that the dwr version is really bad. The official 3.0 series has not yet released the official version, so it is inevitable that there will be a strange problem. If you know something, please let me know and thank you ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.