SpringMVC整合DWR(Maven项目+jetty插件运行)

SpringMVC的maven项目创建可以参考这篇文章:http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357619.html


建立一个SpringMVC项目如下:

SpringMVC整合DWR(Maven项目+jetty插件运行)

SpringMVC整合DWR(Maven项目+jetty插件运行)SpringMVC整合DWR(Maven项目+jetty插件运行)

SpringMVC整合DWR(Maven项目+jetty插件运行)


1.pom.xml

<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>
  <artifactId>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>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
            
    
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    
    <plugins>
      <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>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:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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>
Spring-mvc缺省是在WEB-INF目录下找名称未${servlet-name}-context.xml的配置文件,这里通过contextconfiglocation的配置,将spring-mvc所需的配置文件指向到src\resources\spring\web目录下。


3.servlet-context.xml

<?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" />
    </bean>

</beans>

4.Controller类HelloWorld.java:

package 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:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>


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.csdn.net/geloin/article/details/7537148

SpringMVC整合DWR(Maven项目+jetty插件运行)

1.导入DWR包和其他必须包,pom.xml:

<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>
  <artifactId>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>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
            
      <dependency>
				<groupId>org.directwebremoting</groupId>
				<artifactId>dwr</artifactId>
				<version>3.0.M1</version>
			</dependency>
			

		<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.7.4</version>
</dependency>
            
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    
    <plugins>
      <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>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配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">
  <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的配置,修改后的文件如下所示:

<?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  

">

	<aop:aspectj-autoproxy />
	<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" />
	</bean>

	<!-- DWR配置 -->

	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="order" value="1" />
	</bean>
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="order" value="2" />
	</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>
	</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:controller>

	<dwr:annotation-config id="dwr" />
	<dwr:annotation-scan scanRemoteProxy="true"
		scanDataTransferObject="true" base-package="com.kxw.dwr.model" />

</beans>

4.Controller类:

package 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;
	}
}

package 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
				+ "]";
	}
	
}

package 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:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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>
<body>

</body>
</html>

6.运行项目run:jetty

在浏览器敲入:http://localhost:7878/springmvc_dwr/dwr.jsp

SpringMVC整合DWR(Maven项目+jetty插件运行)SpringMVC整合DWR(Maven项目+jetty插件运行)

连续显示四个对话框

SpringMVC整合DWR(Maven项目+jetty插件运行),布布扣,bubuko.com

SpringMVC整合DWR(Maven项目+jetty插件运行)

上一篇:三周学会小程序第一讲:小程序申请和注意事项


下一篇:(扫码二维码不显示)php微信扫码支付sdk不能用了