首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

容易Spring+hessian

2014-01-03 
简单Spring+hessiandiv stylefont-size: 14px简单的Spring+hessianJar包:dist\modules里面的 spring-

简单Spring+hessian
<div style="font-size: 14px">
简单的Spring+hessian
           
Jar包:
dist\modules里面的 spring-webmvc.jar 
lib\caucho 里面的hessian-3.1.3.jar
Server:
里面有个接口interface:
public interface IBasic {

public String helloWorld();
public User getUser();
}
接口的实现:
public class IBasicServer implements IBasic {
@Override
public String helloWorld() {
return "hello world!!";
}
@Override
public User getUser() {
User user =new User();
user.setName("nie");
user.setPassword("123");
return user;
}
}
建立一个model层:(实现Serializable接口)
public class User implements Serializable {

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String name;
private String password;
}

Web.xml里面配置:
<servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
</servlet-mapping>

在WEB-INF下面创建一个remoting-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="accountService" ref="accountService"/>
    <property name="serviceInterface" value="nie.wei.qing.Server.IBasic"/>
</bean>

</beans>



客户端同样的要有interface  IBasic  和model层  User(实现Serializable接口)
在src下面建立一个remote-client.xml
<bean id="accountService" value="http://localhost:8080/Hissian-Server/remoting/AccountService"/>
    <property name="serviceInterface" value="nie.wei.qing.Server.IBasic"/>
</bean>

使用JUnit来测试;测试代码:
try
{
ApplicationContext context = new ClassPathXmlApplicationContext("remote-client.xml");

IBasic basic =(IBasic)context.getBean("accountService");

System.out.println(basic.helloWorld());
}
catch (Exception e)
{
e.printStackTrace();
}</div>

热点排行