GVKun编程网logo

将Spring ModelAttribute应用于使用特定参数类型的所有控制器(将spring配置应用程序的方式有哪些)

10

对于将SpringModelAttribute应用于使用特定参数类型的所有控制器感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍将spring配置应用程序的方式有哪些,并为您提供关于@Model

对于将Spring ModelAttribute应用于使用特定参数类型的所有控制器感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍将spring配置应用程序的方式有哪些,并为您提供关于@ModelAttribute注解和SpringMVC表单modelAttribute属性、asp.net-mvc-3 – 将AuthorizeAttribute应用于控制器类并同时执行、java – 了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释、java-在控制器中处理完@Mattribute之后,如何在Spring MVC中重置@ModelAttribute?的有用信息。

本文目录一览:

将Spring ModelAttribute应用于使用特定参数类型的所有控制器(将spring配置应用程序的方式有哪些)

将Spring ModelAttribute应用于使用特定参数类型的所有控制器(将spring配置应用程序的方式有哪些)

在SpringBoot
REST应用程序中,我有一个TableRequest类型,其中包含针对表格数据的GET请求的列排序,过滤和分页详细信息。它是通用的,因为它不在乎所请求的特定数据是什么,它仅指定通用表参数。因此,它适用于许多不同的控制器方法。另外,因为它适用于GET请求,所以字段作为请求参数(没有@RequestBodyjson参数)传递。我@ModelAttribute在控制器类中有一个将请求参数解析为TableRequest对象的@RequestMapping方法,然后实际方法将该对象作为@ModelAttribute参数接收。

因为TableRequest该类是通用的,所以我希望能够在多个控制器之间使用它,而不必将解析逻辑复制到每个控制器中。我想知道@ModelAttribute每当控制器具有TableRequest输入参数时,是否存在基于Spring-
y批注的重用相同方法的方法。

提前致谢 :)


我的解决方案 (基于以下选定的答案)

我创建了一个@TableRequestController注释和一个相应的@ControllerAdvice类,该类仅适用于具有该注释的控制器类。该ControllerAdvice类包括将@ModelAttributeGET请求参数解析为一个TableRequest对象的方法。

需要注意的一个重要事项是,新@TableRequestController内容只能整体应用于Controller类,而不能应用于单个控制器方法。这样,我创建了一个单独的内部控制器类,并用该注释标记,该类的@RequestMapping方法都接受TableRequest对象。

@TableRequestController:

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface TableRequestController {}

ControllerAdvice类:

@ControllerAdvice(annotations = TableRequestController.class)public class TableRequestControllerAdvice {    @ModelAttribute    public TableRequest tableRequest(            @RequestParam Map<String, String> params,            @RequestParam int pageStart,            @RequestParam int pageSize) {        return new TableRequest(params, pageStart, pageSize);    }}

TableRequest REST控制器类:

@RestController@TableRequestControllerpublic static class MyTableRequestController {    @RequestMapping("/the/table/request/url")    public MyResponse makeTableRequest(            TableRequest tableRequest) {        return new MyResponse(tableRequest);    }}

答案1

小编典典

您可以使用@ControllerAdvice。如果您愿意,此处定义的所有内容都适用于所有控制器或已定义的子集。

文献资料

另一种选择(更好的恕我直言)是编写消息转换器。它仅处理一种特定类型。您不再需要,@ModelAttribute而只需TableRequest在控制器方法中有一个参数。

@ModelAttribute注解和SpringMVC表单modelAttribute属性

@ModelAttribute注解和SpringMVC表单modelAttribute属性

@ModelAttribute注解

1.没有添加@ModelAttribute修饰的方法,在目标方法的参数对实体进行入参,该实体默认会从ModelMap中获取,如果不存在则创建新的对象作为入参参数。在进行数据绑定的时候(doBinder()方法),会将该实体放入到ModelMap中。

2.添加@ModelAttribute修饰的方法,在每个目标方法调用前都会执行该方法。

一般情况下,在form表单修改的时,某项字段规定为不可更改,就需要使用该注解标注的方法,根据id的获取与否,来从数据库中获取对应的实体,然后存放到ModelMap中。

SpringMVC表单ModelAttribute属性

使用SpringMVC表单标签,必须要在request中有一个和表单对应的bean。默认request的键为command。可以通过修改form标签的modelAttribute属性修改键的值。

@SessionAttribute注解和@ModelAttribute注解冲突:

没有使用@ModelAttribute修饰的方法,且在某个目标方法入参使用了@SessionAttribute注解value同名的实体对象,会抛出异常。

原因:

if (implicitModel.containsKey(name)) {
    bindObject = implicitModel.get(name);
} else if (this.methodResolver.isSessionAttribute(name, paramType)) {
    bindObject = this.sessionAttributeStore.retrieveAttribute(webRequest, name);
    if (bindObject == null) {
    raiseSessionRequiredException("Session attribute ''" + name + "'' required - not found in session");
    }
}
在绑定参数前,此时Model中没有值,然后会进入另一个分支,就会抛出异常。

解决办法:

1.@SesionAttribute和方法入参处使用不同名的value值

2.添加@ModelAttribute修饰的方法,方法内将实体放入ModelMap中。


asp.net-mvc-3 – 将AuthorizeAttribute应用于控制器类并同时执行

asp.net-mvc-3 – 将AuthorizeAttribute应用于控制器类并同时执行

是否有一种方法可以在具有Authorize属性的控制器类中的一个操作中忽略[授权] attibute?
[Authorize]
        public class MyController : Controller
        {
           [Authorize(Users="?")]//I tried to do that and with "*",but unsuccessfuly,public ActionResult Publicmethod()
           {
           //some code
           }

           public ActionResult PrivateMethod()
           {
           //some code
           }
        }

只有PrivateMethod()应​​该需要验证,但它也是必需的.

PS:我不想让我的自定义授权过滤器.

[]的

解决方法

默认情况下是不可能的 – 如果您为控制器设置[授权],则只有经过身份验证的用户可以访问操作.

要么

您可以尝试自定义的决定:stackoverflow.

java – 了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释

java – 了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释

我是Spring MVC的新手.目前我正在研究Spring MVC Showcase,它演示了Spring MVC Web框架的功能.

我有一些问题需要了解如何在此示例中处理自定义可解析的Web参数.

在实践中,我有以下情况.在我的home.jsp视图中,我有以下链接:

此链接生成对URL的HTTP请求:“/ data / custom”

包含处理此请求的方法的控制器类具有以下代码:

@Controller
public class CustomArgumentController {

    @modelattribute
    void beforeInvokingHandlerMethod(HttpServletRequest request) {
        request.setAttribute("foo","bar");
    }

    @RequestMapping(value="/data/custom",method=RequestMethod.GET)
    public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
        return "Got 'foo' request attribute value '" + foo + "'";
    }

}

处理此HTTP请求的方法是custom().因此,单击上一个链接时,HTTP请求将由自定义方法处理.

我有一些问题需要了解@RequestAttribute注释到底做了什么.我认为,在这种情况下,它将名为foo的请求属性绑定到一个新的String foo变量.但是这个属性取自何处?这个变量是Spring采用的吗?

好的,我的想法是这个请求属性取自HttpServletRequest对象.我是这么认为的,因为在这个类中,我还有一个具有说话名称的beforeInvokingHandlerMethod()方法,所以看起来这个方法在HttpServletRequest对象中设置了一个名称= foo和value = bar的属性,然后所以custom()方法可以使用这个值.

实际上我的输出是:

Got ‘foo’ request attribute value ‘bar’

为什么在custom()方法之前调用beforeInvokingHandlerMethod()?

为什么beforeInvokingHandlerMethod()由@modelattribute注释注释?在这种情况下它意味着什么?

最佳答案
RequestAttribute只是您在表单提交中传递的参数.让我们了解一个示例示例

假设我有以下表格

aram1 value=test/>

现在,如果我有下面的控制器,它与请求URL映射,并与表单提交映射,如下所示.

@Controller
public class CustomArgumentController {

@modelattribute
void beforeInvokingHandlerMethod(HttpServletRequest request) {
    request.setAttribute("foo","bar");
}


@RequestMapping(value="/data/custom",method=RequestMethod.GET)
public @ResponseBody String custom(@RequestAttribute("param1") String param1 ) {
    // Here,I will have value of param1 as test in String object which will be mapped my Spring itself
}

java-在控制器中处理完@Mattribute之后,如何在Spring MVC中重置@ModelAttribute?

java-在控制器中处理完@Mattribute之后,如何在Spring MVC中重置@ModelAttribute?

我定义了一个@modelattribute(“ mymodel”)

@modelattribute("mymodel")
MyModel mymodel() {
  MyModel mymodel = new MyModel();
  return mymodel;
 }


@RequestMapping(value = "/save",method = RequestMethod.POST)
public final void save(@modelattribute("mymodel") MyModel mymodel,final BindingResult binding,final HttpServletRequest request,final ModelMap modelMap) throws Exception {
    modelService.save(mymodel);

            // try to reset the model --> doesn't work!!!
    myModel = new MyModel();
}

问题是,即使我在save方法中重置了模型,但如果在保存操作之后重新加载页面并再次保存,该模型将包含先前myModel的所有值.

处理后如何重置?

最佳答案
除非我错过我的猜测,否则是因为

myModel = new MyModel();

只会以与从List< MyModel>中获取MyModel相同的方式来重置方法中的引用.然后调用myModel = new MyModel();不会更改列表中的元素,仅更改本地参考.

您很可能需要将新的MyModel()放入模型或modelMap中.

发布后重定向模式在这里也可能对您有用.有你的POST方法

返回“ redirect:originalpage.htm”

这应该重新加载原始页面,这也意味着如果单击刷新,则不会重新提交POST,将对象保存两次.

我们今天的关于将Spring ModelAttribute应用于使用特定参数类型的所有控制器将spring配置应用程序的方式有哪些的分享就到这里,谢谢您的阅读,如果想了解更多关于@ModelAttribute注解和SpringMVC表单modelAttribute属性、asp.net-mvc-3 – 将AuthorizeAttribute应用于控制器类并同时执行、java – 了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释、java-在控制器中处理完@Mattribute之后,如何在Spring MVC中重置@ModelAttribute?的相关信息,可以在本站进行搜索。

本文标签: