DWR Spring 集成:是否可以将 DWR 请求转发到 Spring 控制器?

发布于 2024-10-02 04:26:45 字数 893 浏览 4 评论 0原文

我有一个集成了 DWR 3 和 Spring 3 的 Web 应用程序。所有请求都由 Spring 的 DispatcherServlet 处理。一切正常。当我请求 AJAX 请求时,用 @RemoteProxy 注释的支持 DWR 服务 bean 会正确处理它。为了返回响应,我的 DWR 服务 bean 返回包含 @DataTransferObject POJO 的 HTML 字符串或仅返回普通 POJO。

我想要做的是在 DWR 服务 bean 中,我希望它将处理转发给 Spring @Controller bean。 AJAX 请求仍将由 DWR 服务处理,但真正的处理委托给 Spring 控制器 bean。换句话说,DWR 服务 bean 只是实际服务的服务外观。这样我就不会重复逻辑。

这可能吗?

让我进一步澄清。

在普通的非 AJAX 应用程序中,当用户提交表单时,会发生以下情况:

  1. 它被转发到 DispatcherServlet
  2. 然后是一个 @Controller 注解的 bean。
  3. 然后由@Service bean 进行处理。
  4. 之后,控制器返回一个 ModelAndView。

在 DWR-AJAX 应用程序中,当用户提交表单时,会发生以下情况:

  1. 它仍然转发到 DispatcherServlet 然后
  2. 转发到带注释的 @RemoteProxy 豆。处理由 这颗豆子。这就是DWR的服务 豆。
  3. 之后,这个远程代理 bean 返回一个 @DataTransferObject POJO 或只是简单的 HTML 字符串

基本上对于 AJAX 应用程序, 第 2 步之后,我希望它转发 到@Controller bean,以便 一切仍在处理中 春天。

I have a web application that integrates DWR 3 and Spring 3. All requests are handled by the Spring's DispatcherServlet. Everything works. When I request an AJAX request, it's handled correctly by the backing DWR service bean annotated with @RemoteProxy. To return a response, my DWR service bean returns either an HTML string including the @DataTransferObject POJO or just the plain POJO.

What I want to do is in the DWR service bean I want it to forward the processing to a Spring @Controller bean. The AJAX request will still be processed by the DWR service but the real processing is delegated to the Spring controller bean. In other words the DWR service bean is just a service facade to the actual service. In this way I'm not duplicating logic.

Is this possible?

Let me clarify further.

In a normal non-AJAX application, when a user submits a form, here's what happens:

  1. It's forwarded to the
    DispatcherServlet
  2. Then to an @Controller annotated bean.
  3. The processing is then handled by @Service bean.
  4. Afterwards, the controller returns a ModelAndView.

In a DWR-AJAX application, when a user submits a form, here's what happens:

  1. It's forwarded to the DispatcherServlet still
  2. Then to a @RemoteProxy annotated
    bean. The processing is handled by
    this bean. That's DWR's service
    bean.
  3. Afterwards, this remote proxy bean returns either an @DataTransferObject POJO or just plain HTML string

Basically for the AJAX application,
after step 2, I want it to forward
to the @Controller bean so that
everything is still processed by
Spring.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2024-10-09 04:26:45

简短的回答是否定的。

DWR 请求中没有 Spring 控制器的位置。 DWR 对远程处理 spring beans 有很大的支持,但是控制器可以为这个等式带来很少的价值。

也就是说,我没有理由认为 Spring 3.0 风格的带注释的控制器不能用作公开的 DWR bean。限制是返回类型不会是 ModelAndView 而是它本身的模型对象。

Short answer is no.

There is no place in a DWR request for a spring controller. DWR has great support for remoting spring beans, but there is little value that a controller could bring to this equation.

That said there is no reason that I can think of that a spring 3.0 style annotated controller couldn't be used as the exposed DWR bean. The restriction would be that the return type would not be ModelAndView rather it would just be your model object it's self.

野生奥特曼 2024-10-09 04:26:45

我对在这种情况下使用注释不太了解。使用声明性方法,您可以执行以下操作。

在 spring XML 中(注意 fileOperationService 是 spring bean)

    <bean name="BookRequestAjax" class="com.bookie.struts.BookRequestAjax">
        <property name="fileOperationService" ref="fileOperationService"/>
    </bean>

你的 bean

public class BookRequestAjax {
    FileOperationService fileOperationService;
    public void deleteFile(String fileName){
        try{
            fileOperationService.deleteFile(fileName);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void setFileOperationService(FileOperationService fileOperationService) {
        this.fileOperationService = fileOperationService;
    }

}

你的 DWR.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
    <allow>
    <create creator="spring" javascript="BookRequestAjax">
      <param name="beanName" value="BookRequestAjax" />
    </create>
    </allow>
</dwr>

I don't know much about using annotations in this case. With the declarative approach you can do the following.

In spring XML(Note that fileOperationService is the spring bean)

    <bean name="BookRequestAjax" class="com.bookie.struts.BookRequestAjax">
        <property name="fileOperationService" ref="fileOperationService"/>
    </bean>

Your bean

public class BookRequestAjax {
    FileOperationService fileOperationService;
    public void deleteFile(String fileName){
        try{
            fileOperationService.deleteFile(fileName);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void setFileOperationService(FileOperationService fileOperationService) {
        this.fileOperationService = fileOperationService;
    }

}

Your DWR.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
    <allow>
    <create creator="spring" javascript="BookRequestAjax">
      <param name="beanName" value="BookRequestAjax" />
    </create>
    </allow>
</dwr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文