Spring控制器中是否可以有handleRequest函数以及参数注释?

发布于 2024-11-17 23:22:25 字数 530 浏览 0 评论 0原文

我刚刚开始使用 spring 并在 NetBeans 中建立了一个简单的 spring 项目。默认情况下它似乎不使用注释。

在我的一个控制器中,我想处理表单上的不同点击。我看过一些解决方案,似乎添加参数检查可以工作:

@RequestMapping(params = "someAction")
public ModelAndView doSomeAction() {

但是看起来所有请求都会:

public class SetupController implements Controller {
@Override
public org.springframework.web.servlet.ModelAndView handleRequest(
        HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

有没有一种方法可以在没有 RequestMapping 注释的情况下达到相同的效果?

I just started working with spring and set up a simple spring project in NetBeans. By default it doesn't seem to use annotations.

In one of my controllers I'd like to handle different clicks on the form. I have looked at some solutions and it seems like adding a parameter check could work:

@RequestMapping(params = "someAction")
public ModelAndView doSomeAction() {

However it looks like all of the requests are going to:

public class SetupController implements Controller {
@Override
public org.springframework.web.servlet.ModelAndView handleRequest(
        HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

Is there a way I can achieve the same effect without RequestMapping annotation?

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

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

发布评论

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

评论(1

不及他 2024-11-24 23:22:25

看来您将旧的(实现控制器)样式与新的基于注释的样式混合在一起。

你不能(或者至少不应该)混合它们,至少在一门课上不能。

如果您使用 Spring 3.x,那么我强烈建议您使用基于注释的样式。

@Controller
@RequestMapping("/whatever")
public class DemoController() {

   @RequestMapping(params="x");
   public ModelAndView doX() {
   }

   @RequestMapping(params="y");
   public ModelAndView doY() {
   }
}

要启用基于注释的样式,您至少需要以下配置设置:

<!-- Turns on support for mapping requests to Spring MVC @Controller methods 
  Also registers default Formatters and Validators for use across all
      @Controllers -->
<mvc:annotation-driven conversion-service="applicationConversionService" />

It seams that you mixed the old (implements Controller) style with the new one annotation based style.

You can't (or at least should not) mix them, at least not for one class.

If you use Spring 3.x then I strongly recommend to use the Annotation based style.

@Controller
@RequestMapping("/whatever")
public class DemoController() {

   @RequestMapping(params="x");
   public ModelAndView doX() {
   }

   @RequestMapping(params="y");
   public ModelAndView doY() {
   }
}

To enable the Annotation Based style, you need at least this configuration settings:

<!-- Turns on support for mapping requests to Spring MVC @Controller methods 
  Also registers default Formatters and Validators for use across all
      @Controllers -->
<mvc:annotation-driven conversion-service="applicationConversionService" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文