-
SpringMVC的maven项目创建可以参考这篇文章:http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357619.html
建立一个SpringMVC项目如下:
1.pom.xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172<project xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelversion>
4.0
.
0
</modelversion>
<groupid>com.kxw.web</groupid>
springmvc</artifactid>
<packaging>war</packaging>
<version>
0.0
.
1
-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http:
//maven.apache.org</url>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https:
//nexus.sourcesense.com/nexus/content/repositories/public/</url>
<layout>
default
</layout>
<snapshots>
<enabled>
false
</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceencoding>UTF-
8
</project.build.sourceencoding>
<org.springframework.version>
3.0
.
5
.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
junit</artifactid>
<version>
4.10
</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
spring-webmvc</artifactid>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
<build>
<finalname>springmvc</finalname>
<plugins>
<plugin>
<groupid>org.mortbay.jetty</groupid>
jetty-maven-plugin</artifactid>
<version>
8.1
.
14
.v20131031</version>
<configuration>
<scanintervalseconds>
200
</scanintervalseconds>
<webappconfig>
<contextpath>/springmvc</contextpath>
<defaultsdescriptor>src/main/resources/webdefault.xml</defaultsdescriptor>
</webappconfig>
<connectors>
<connector implementation=
"org.eclipse.jetty.server.nio.SelectChannelConnector"
>
<port>
7878
</port>
<maxidletime>
60000
</maxidletime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.web.xml:1234567891011121314151617181920<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvcServlet</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/servlet-context.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvcServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
3.servlet-context.xml
1234567891011121314151617<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:context=
"http://www.springframework.org/schema/context"
xsi:schemalocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-
package
=
"com.kxw.springmvc.controller"
>
<bean id=
"viewResolver"
class
=
"org.springframework.web.servlet.view.UrlBasedViewResolver"
>
<property name=
"viewClass"
value=
"org.springframework.web.servlet.view.JstlView"
>
<property name=
"prefix"
value=
"/WEB-INF/jsp/"
>
<property name=
"suffix"
value=
".jsp"
>
</property></property></property></bean>
</context:component-scan></beans>
4.Controller类HelloWorld.java:1234567891011121314151617package
com.kxw.springmvc.controller;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping
(
"/helloworld"
)
public
class
Helloworld {
@RequestMapping
(method=RequestMethod.GET)
public
ModelAndView hello() {
ModelAndView mv =
new
ModelAndView();
mv.setViewName(
"helloworld"
);
return
mv;
}
}
5.helloworld.jsp:1234567891011<%
@page
contentType=
"text/html"
pageEncoding=
"UTF-8"
%>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"
>
<title>JSP Page</title>
<h1>Hello World!</h1>
6.webdefault.xml:
可去下载jetty-distribution-7.6.12.v20130726.zip,里面便有这个文件
7.运行mvn jetty:run 启动工程后,访问http://localhost:7878/springmvc/helloworld.action ,可以看到Hello World!
创建一个SpringMVC项目之后,接下来来整合DWR
可参考此文章:http://blog.youkuaiyun.com/geloin/article/details/7537148
1.导入DWR包和其他必须包,pom.xml:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384<project xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelversion>
4.0
.
0
</modelversion>
<groupid>com.kxw.web</groupid>
springmvc</artifactid>
<packaging>war</packaging>
<version>
0.0
.
1
-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http:
//maven.apache.org</url>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https:
//nexus.sourcesense.com/nexus/content/repositories/public/</url>
<layout>
default
</layout>
<snapshots>
<enabled>
false
</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceencoding>UTF-
8
</project.build.sourceencoding>
<org.springframework.version>
3.0
.
5
.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
junit</artifactid>
<version>
4.10
</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
spring-webmvc</artifactid>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupid>org.directwebremoting</groupid>
dwr</artifactid>
<version>
3.0
.M1</version>
</dependency>
<dependency>
<groupid>org.aspectj</groupid>
aspectjweaver</artifactid>
<version>
1.7
.
4
</version>
</dependency>
</dependencies>
<build>
<finalname>springmvc</finalname>
<plugins>
<plugin>
<groupid>org.mortbay.jetty</groupid>
jetty-maven-plugin</artifactid>
<version>
8.1
.
14
.v20131031</version>
<configuration>
<scanintervalseconds>
200
</scanintervalseconds>
<webappconfig>
<contextpath>/springmvc_dwr</contextpath>
<defaultsdescriptor>src/main/resources/webdefault.xml</defaultsdescriptor>
</webappconfig>
<connectors>
<connector implementation=
"org.eclipse.jetty.server.nio.SelectChannelConnector"
>
<port>
7878
</port>
<maxidletime>
60000
</maxidletime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.在web.xml中配置dwr。只需在配置DispatcherServlet下添加dwr的url-mapping即可,修改后的DispatcherServlet配置如下所示:
123456789101112131415161718192021<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvcServlet</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/servlet-context.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvcServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvcServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
3. 在servlet-context.xml中添加dwr的配置,修改后的文件如下所示:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:dwr=
"http://www.directwebremoting.org/schema/spring-dwr"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xsi:schemalocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//www.directwebremoting.org/schema/spring-dwr
http:
//directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<mvc:annotation-driven>
<context:component-scan base-
package
=
"com.kxw.springmvc.controller"
>
<context:component-scan base-
package
=
"com.kxw.dwr.model"
>
<bean id=
"viewResolver"
class
=
"org.springframework.web.servlet.view.UrlBasedViewResolver"
>
<property name=
"viewClass"
value=
"org.springframework.web.servlet.view.JstlView"
>
<property name=
"prefix"
value=
"/WEB-INF/jsp/"
>
<property name=
"suffix"
value=
".jsp"
>
</property></property></property></bean>
<!-- DWR配置 -->
<bean
class
=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
>
<property name=
"order"
value=
"1"
>
</property></bean>
<bean
class
=
"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
>
<property name=
"order"
value=
"2"
>
</property></bean>
<bean
class
=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
>
<bean
class
=
"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"
>
<bean
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<property name=
"order"
value=
"3"
>
<property value=
"true"
name=
"alwaysUseFullPath"
></property>
<property name=
"mappings"
>
<props>
<prop key=
"/dwr/**"
>dwrController</prop>
</props>
</property>
</property></bean>
<dwr:configuration>
<dwr:convert type=
"bean"
class
=
"com.kxw.dwr.model.User"
></dwr:convert>
<dwr:convert type=
"bean"
class
=
"com.kxw.dwr.model.Group"
></dwr:convert>
</dwr:configuration>
<dwr:controller id=
"dwrController"
debug=
"true"
>
<dwr:config-param name=
"allowScriptTagRemoting"
value=
"true"
>
<dwr:config-param name=
"crossDomainSessionSecurity"
value=
"false"
>
</dwr:config-param></dwr:config-param></dwr:controller>
<dwr:annotation-config id=
"dwr"
>
<dwr:annotation-scan scanremoteproxy=
"true"
scandatatransferobject=
"true"
base-
package
=
"com.kxw.dwr.model"
>
</dwr:annotation-scan></dwr:annotation-config></bean></bean></context:component-scan></context:component-scan></mvc:annotation-driven></aop:aspectj-autoproxy></beans>
4.Controller类:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859package
com.kxw.dwr.model;
import
java.util.ArrayList;
import
java.util.List;
import
org.directwebremoting.annotations.RemoteMethod;
import
org.directwebremoting.annotations.RemoteProxy;
import
org.springframework.stereotype.Controller;
@Controller
@RemoteProxy
(name=
"MyDwr"
)
public
class
MyDwr {
@RemoteMethod
public
String hello(String world){
System.out.println(
"hello"
+world);
return
"hello"
+world;
}
@RemoteMethod
public
User load(){
User user=
new
User(
10
,
"Messi"
,
new
Group(
1
,
"Bacelona FC"
));
return
user;
}
@RemoteMethod
public
List<user> list(){
List<user> users=
new
ArrayList<user>();
users.add(
new
User(
7
,
"Ronaldo"
,
new
Group(
3
,
"Real Madrid"
)));
users.add(
new
User(
11
,
"Ozil"
,
new
Group(
8
,
"Asenal"
)));
users.add(
new
User(
20
,
"Van Persie"
,
new
Group(
2
,
"Manchester United"
)));
users.add(
new
User(
9
,
"Torress"
,
new
Group(
5
,
"Chelsea"
)));
return
users;
}
@RemoteMethod
public
void
add(User user){
System.out.println(user);
}
@RemoteMethod
public
void
deleteUser(){
throw
new
MyException(
"删除用户!!"
);
}
@RemoteMethod
public
int
add(
int
a,
int
b){
return
a+b;
}
}
</user></user></user>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647package
com.kxw.dwr.model;
public
class
User {
private
int
id;
private
String username;
private
Group group;
public
User(
int
id, String username, Group group) {
super
();
this
.id = id;
this
.username = username;
this
.group = group;
}
public
User() {
super
();
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
Group getGroup() {
return
group;
}
public
void
setGroup(Group group) {
this
.group = group;
}
@Override
public
String toString() {
return
"User [id="
+ id +
", username="
+ username +
", group="
+ group
+
"]"
;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142package
com.kxw.dwr.model;
public
class
Group {
private
int
id;
private
String name;
public
Group(
int
id, String name) {
super
();
this
.id = id;
this
.name = name;
}
public
Group() {
super
();
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
@Override
public
String toString() {
return
"Group [id="
+ id +
", name="
+ name +
"]"
;
}
}
5.dwr.jsp:
1234567891011121314151617181920<%@ page language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"
>
<title>Insert title here</title>
<script type=
"text/javascript"
src=
"<%=request.getContextPath()%>/dwr/engine.js"
></script>
<script type=
"text/javascript"
src=
"<%=request.getContextPath()%>/dwr/util.js"
></script>
<script type=
"text/javascript"
src=
"<%=request.getContextPath()%>/dwr/interface/MyDwr.js"
></script>
<script type=
"text/javascript"
>
MyDwr.list(listUser);
function listUser(users){
for
(var i=
0
;i<users.length;i++){ alert(users[i].username+
","
+users[i].group.name);=
""
}=
""
<=
""
script>=
""
<=
""
head=
""
>
</users.length;i++){>
6.运行项目run:jetty在浏览器敲入:http://localhost:7878/springmvc_dwr/dwr.jsp
连续显示四个对话框
SpringMVC整合DWR(Maven项目+jetty插件运行)
最新推荐文章于 2022-08-24 15:12:36 发布