Close

Spring MVC - @ModelAttribute Examples

Spring MVC 

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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("users")
public class UserController {
@Autowired
private UserService userService;

@RequestMapping("{userId}")
public String handleRequestById (@ModelAttribute("user") User user, Model model) {
model.addAttribute("msg", "user : " + user);
return user!=null && "admin".equals(user.getRole()) ? "admin-page" : "user-page";
}

@ModelAttribute("user")
public User getUser (@PathVariable("userId") long userId) {
return userService.getUserById(userId);
}
}
Original Post




import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.logging.Logger;

@Controller
@RequestMapping("users")
public class UserController {
private static final Logger LOGGER = Logger.getLogger(UserController.class.getName());

private static int counter = 0;

@RequestMapping
public String handleRequest (Model model) {
model.addAttribute("msg", "user request received");
LOGGER.info(model.toString());
return "my-page";
}

@RequestMapping("{id}")
public String handleRequestById (@PathVariable("id") String id, Model model) {
model.addAttribute("msg", "user request received for the id : " + id);
LOGGER.info(model.toString());
return "my-page";
}

@ModelAttribute("time")
public LocalDateTime getRequestTime () {
return LocalDateTime.now();
}

@ModelAttribute("visits")
public int getRequestCount () {
return ++counter;
}

@ModelAttribute("querier")
public void populateIds (@RequestParam(value = "querier", required = false) String querier,
Model model) {
model.addAttribute("querier", querier == null ? "quest" : querier);
}
}
Original Post




    @ModelAttribute("visitor")
public Visitor getVisitor (HttpServletRequest request) {
return new Visitor(request.getRemoteAddr());
}
}
Original Post




See Also