How to use @ModelAttribute annotation in SpringMVC

How to use @ModelAttribute annotation in SpringMVC

I did not use @ModelAttribute but model.addAttribute~ to store the action form instance.

package jp.co.confrage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("sample")
public class HomeController {

  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

  @RequestMapping(value = "home", method = RequestMethod.GET)
  public String home(Model model) {
    logger.info("Welcome Sample Page.");
    model.addAttribute(new HomeForm());
    return "home";
  }
}

Change the action form to the argument of the home method and comment out the model.addAttribute~ line.

package jp.co.confrage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("sample")
public class HomeController {

  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

  @RequestMapping(value = "home", method = RequestMethod.GET)
  public String home(Model model,HomeForm form) {
    logger.info("Welcome Sample Page.");
    //        model.addAttribute(new HomeForm());
    return "home";
  }
}

By specifying it as an argument, there is no need to explicitly model.addAttribute.

It is as if model.addAttribute(new HomeForm()); is executed behind the scenes.

※model.addAttribute(“homeForm”,new HomeForm()); // Same.

On the JSP side, nothing is changed and homeForm is specified for the modelAttribute attribute.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>

<P> Welcome Sample Page. </P>
<spring:url value="/sample/post" var="action" />
<form:form action="${action}" method="post" modelAttribute="homeForm">
<form:label path="keyword">入力›</form:label>
<form:input path="keyword" />
<form:errors path="keyword" cssStyle="color:red" />
<input type="submit" value="送信" />
</form:form>
</body>
</html>

Now let’s try using the @ModelAttribute annotation. Put it in front of the argument.

package jp.co.confrage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("sample")
public class HomeController {

  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

  @RequestMapping(value = "home", method = RequestMethod.GET)
  public String home(Model model,@ModelAttribute("aForm")HomeForm form) {
    logger.info("Welcome Sample Page.");

    //        model.addAttribute(new HomeForm());

    return "home";
  }
}

If this is not done, an error will occur on the JSP side saying “Neither BindingResult nor plain target object for bean name ‘homeForm’ available as request attribute,” so we will write aForm in the modelAttribute attribute.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>

<P> Welcome Sample Page. </P>
<spring:url value="/sample/post" var="action" />
<form:form action="${action}" method="post" modelAttribute="aForm">
<form:label path="keyword">入力›</form:label>
<form:input path="keyword" />
<form:errors path="keyword" cssStyle="color:red" />
<input type="submit" value="送信" />
</form:form>
</body>
</html>

This will change the attribute name and work correctly.

Can specify @ModelAttribute annotation for methods

The @ModelAttribute annotation has a different meaning when specified above the method.

package jp.co.confrage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("sample")
public class HomeController {

  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  @AutoWired
  HomeForm bean; // DI with @AutoWired if you want to include form values.
  @RequestMapping(value = "home", method = RequestMethod.GET)
  public String home(HomeForm hf) { // The setter is called by specifying it as an argument.
    logger.info("Welcome Sample Page.");
    return "home";
  }
  @ModelAttribute
  public HomeForm setForm(){ // Method name can be anything.
    return this.bean;
  }
}

The setForm method is executed and returns an instance. Next, the home method is executed, which also works correctly.

At this time, the modelAttribute attribute on the JSP side must specify homeForm (it must be the camelCase of the action form (bean) class).

What is the role of the @ModelAttribute annotation?

As above, the @ModelAttribute annotation is used to link jsp and action forms (easier to say “bean”?) This annotation is simply a linkage with the specified alias of the

Using @ModelAttribute annotation in SpringMVC

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL