Hessian知识学习总结——Spring + Hessian + Spring MVC

Posted wodediqizhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hessian知识学习总结——Spring + Hessian + Spring MVC相关的知识,希望对你有一定的参考价值。

一、创建Hessian的Server


1.1创建web项目HessionSer,其pom.xml配置如下:

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<argLine>-Dfile.encoding=UTF-8</argLine>
		<spring.version>4.1.6.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.annotation</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.ejb</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.weld</groupId>
			<artifactId>weld-osgi-bundle</artifactId>
			<version>1.0.1-SP3</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.servlet</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.38</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>$spring.version</version>
		</dependency>
	</dependencies>

1.2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        	classpath*:spring*.xml
        </param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:remoting-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
</web-app>

1.3.Hessian的配置文件:remoting-servlet.xml。

因为web.xml里的servlet为remoting,默认会查找servlet的name-servlet.xml的文件,即remoting-servlet.xml,而上面配置里通过contextConfigLocation去查找,所以,此时该配置文件名字可以任意设置。内容如下:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 接口的具体实现类 -->
	<!-- <bean id="loginTestInterfa" class="com.cn.wddqz.manage.impl.LoginTestImpl" /> -->
	<!-- 使用Spring的HessianServie做代理 -->
	<bean name="/loginTestInterfa"
	class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!-- service引用具体的实现实体Bean-->
		<property name="service" ref="loginTestInterfa" />
		<property name="serviceInterface" value="com.cn.wddqz.manage.LoginTestInterfa" />
	</bean>

</beans>

1.4.因为用到spring注解,所以配置spring-servlet.xml:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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-3.1.xsd">

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.cn.wddqz"></context:component-scan>

	<!-- 完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
</beans>

1.5.定义一个接口

package com.cn.wddqz.manage;

/**
 * Hessian Server的接口需要暴露给client使用
 * 类名称:LoginTestInterfa
 * 类描述:
 * 创建人:wodediqizhang@163.com
 * 修改时间:2016-6-20 下午4:22:02
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb            2016-6-20        创建文档 
 * =============================================================================
 * @version 1.0.0
 *
 */
public interface LoginTestInterfa 

	public String qualifyInfo(String name,String mail) ;


1.6.接口的实现:

package com.cn.wddqz.manage.impl;

import com.cn.wddqz.manage.LoginTestInterfa;
import org.springframework.stereotype.Service;
/**
 * Hessian Server的接口实现,client调用接口,会在这里进行业务处理
 * 类名称:LoginTestImpl
 * 类描述:
 * 创建人:wodediqizhang@163.com
 * 修改时间:2016-6-20 下午4:22:44
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb            2016-6-20        创建文档 
 * =============================================================================
 * @version 1.0.0
 *
 */
@Service("loginTestInterfa")
public class LoginTestImpl implements LoginTestInterfa 

	@Override
	public String qualifyInfo(String name, String mail) 
		System.out.println("Hessian Server 接口调用");
		return "名字:["+name+"],邮箱:["+mail+"]";
	


此时,Hessian的Server端已经完成了。因为Hessian是接口暴露,所以将HessianSer的接口导出为jar,提供给HessianCli使用。

二、创建Hessian的Client


2.1.pom.xml加载jar包:

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<argLine>-Dfile.encoding=UTF-8</argLine>
		<spring.version>4.1.6.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.annotation</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.ejb</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.weld</groupId>
			<artifactId>weld-osgi-bundle</artifactId>
			<version>1.0.1-SP3</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.servlet</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.38</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>$spring.version</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>$spring.version</version>
		</dependency>              <dependency>
			<groupId>hessianSer</groupId>
			<artifactId>hessianSer</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency><pre name="code" class="java"></dependencies>

 
 
 
 

2.2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
        	classpath*:context.xml
        </param-value>
	</context-param>
	<!-- Start 配置上下文载入器 -->
	<!-- 上下文载入器载入除DispatcherServlet载入的配置文件之外的其它上下文配置文件 -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml 
			<init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value>  
			默认 </init-param> -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 最常用的上下文载入器是一个Servlet监听器,其名称为ContextLoaderListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- End 配置上下文载入器 -->
</web-app>


2.3.context.xml,客户端关于Hessian的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	 <bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath:hessian.properties</value>
			</list>
		</property>
	</bean>
	<!--客户端Hessian代理工厂Bean-->
	<bean id="loginTestInterfa"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!--这是因为接口中出现方法重载,在调用时,服务器端会跑出异常 。在整合spring中,在客户端的配置里面加上如下代码可以解决:-->
		<property name="overloadEnabled" value="true" />
		<!--请求代理Servlet路径:-->
		<property name="serviceUrl" value="$hessian.urlloginTestInterfa" />
		<!--接口定义:-->
		<property name="serviceInterface"
			value="com.cn.wddqz.manage.LoginTestInterfa" />
	</bean> 

</beans>

2.4.hessian.properties配置Hessian链接,内容如下:


#配置文件
hessian.url=http\\://localhost\\:8080/HessianSer/remoting/

2.5.servlet配置文件spring-servlet.xml:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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-3.1.xsd">

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.cn.wddqz.web"></context:component-scan>

	<!-- 完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	 <!-- 定义JSP -->
	 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

2.6.写个简单的jsp,index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>测试Hessian</title>
  </head>
  
    <body>
    <div style="TEXT-ALIGN: center;MARGIN-RIGHT: auto; MARGIN-LEFT: auto; ">
		<form action="login" method="post">
			<div>
				<label for="name">姓名:</label> <input class="easyui-validatebox"
					type="text" name="name" data-options="required:true" />
			</div>
			<div>
				<label for="email">邮箱:</label> <input class="easyui-validatebox"
					type="text" name="email" data-options="validType:'email'" />
			</div>
			<div>
				<button type="submit">登录</button>
			</div>
		</form>
	</div>
  </body>
</html>

2.7.IndexController调用hessian接口

package com.cn.wddqz.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cn.wddqz.manage.LoginTestInterfa;

/**
 * 通过Controller调用hessian接口
 * 类名称:IndexController
 * 类描述:
 * 创建人:wodediqizhang@163.com
 * 修改时间:2016-6-20 下午4:22:02
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb            2016-6-20        创建文档 
 * =============================================================================
 * @version 1.0.0
 *
 */
@Controller
public class IndexController 

	@Autowired
	private LoginTestInterfa loginTestInterfa;
	@RequestMapping("/login")
	public ModelAndView indexView(HttpServletRequest request) 
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String loginInfo = loginTestInterfa.qualifyInfo(name,email);
		System.out.println("loginInfo="+loginInfo);
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", loginInfo);
		return mv;
	


2.8.login.jsp显示调用接口之后在页面上展示message数据:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>显示内容</title>
  </head>
  
  <body>
   $message ;
  </body>
</html>


2.9.将HessianSer导出的jar包导出到本地,然后通过myeclipse导入到maven库里面。在HessianCli的pom.xml配置jar包即可。这样子方便以后HessianSer的接口增多时,只需要将接口导出,然后替换maven库中的jar包就能完成项目更新而不需要在修改client端的配置。

此时HessianCli就配置完成了。

三、测试

    访问http://127.0.0.1:8080/HessianCli/,得到如下界面:
点击登录,返回到login页面,
此时,测试已完成。

最后,附上源码链接: Spring+Hessian.zip。












以上是关于Hessian知识学习总结——Spring + Hessian + Spring MVC的主要内容,如果未能解决你的问题,请参考以下文章

Hessian知识学习总结——基础知识

Hessian知识学习总结——Hessian的helloworld

Hessian知识学习总结——Hessian的helloworld

机器学习中的矩阵求导Jacobian矩阵和Hessian矩阵

Hessian入门(包括与Spring集成)

Dubbo亮点总结