springmvc+HttpInvok远程调用

把view+controller层放在web工程中。。service+dao+po放在svc工程中。。

请求controller,在controller中调用业务逻辑service,web工程中只有svc工程中的接口(可以用jar包),在配置bean的时候把controller中的变量配置成proxy,实际上引用远程svc中的类。

具体实现,客户端步骤:

web.xml:拦截请求(与springmvc配置相同)

 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

springmvc-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.1.xsd
          
http://www.springframework.org/schema/context
          
http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc
          
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" default-autowire="byName">

 <context:component-scan base-package="com.springmvc2"/>
</beans>

jsp请求访问 <a href="controller/hello.do">showALL</a>

根据注解找到controller和方法

@org.springframework.stereotype.Controller
@RequestMapping(value="/controller")
public class HelloController {
IDAO dao;//不要忘记setter/getter;
@RequestMapping(value="/hello.do")
 public String sayHello(HttpServletRequest request,HttpServletResponse response,PrintWriter out){
  System.out.println("hello1");
  System.out.println("对象:+dao);
  dao.getAllCategory();

接下来使最主要的配置application.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: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/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" default-autowire="byName">

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>

   <bean id="dao" class="com.marathon.util.spring.StreamSupportingHttpInvokerProxyFactoryBean">
  <property name="serviceUrl" value="
http://localhost:8080/springmvc2_svc/test/CategoryService" />
  <property name="serviceInterface" value="com.newland.mybatis.dao.IDAO" />
   </bean>
</beans>

为上面的dao变量配置bean,(所有在远程传输中的对象都要序列化,所以class DAO implements IDAO,Serializable)
这样dao就会请求
http://localhost:8080/springmvc2_svc/test/CategoryService

服务端的配置如下:

首先配置web.xml 拦截请求/test/*(所以刚刚的请求/test/CategoryService就能被拦截)
  <servlet>
  <servlet-name>spring3-service</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring3-service</servlet-name>
  <url-pattern>/test/*</url-pattern>
 </servlet-mapping>


spring3-service-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="true">
 
 <bean name="/CategoryService" class="com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter">
  <property name="service" ref="categoryDao" />
  <property name="serviceInterface" value="com.newland.mybatis.dao.IDAO" />
 </bean>

 <bean id="categoryDao" class="com.newland.mybatis.dao.DAO">
  </bean>
</beans>

这样就配置好了。。。客户端的dao最终变成了调用服务端的dao了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值