Spring #SessionAttributes returns null - java

Im trying to use #SessionAttributes in my controller class as follows:
#Controller
#SessionAttributes({"calMethods", "taxList"})
#RequestMapping("/secure")
public class reportController extends BaseController {
//..
#ModelAttribute("taxList")
public List<multiCalDto> getTaxList() {
return new ArrayList<multiCalDto>();
}
//....
#RequestMapping(value = "/confirmCal.html", method = RequestMethod.GET)
public ModelAndView launchconfirmCal(HttpServletRequest request, #RequestParam("seqNo") String seqNo) {
...........
ModelAndView modelAndView = new ModelAndView("confirmCalView");
modelAndView.addObject("taxList", calBean.getTaxList());
return modelAndView;
}
#RequestMapping(value = "/executeCalPay.html", method = RequestMethod.POST)
public ModelAndView executeCalPay(HttpServletRequest request, #ModelAttribute("taxList") List<multiCalDto> taxList) {
// ............
ModelAndView modelAndView = new ModelAndView("calView");
System.out.println("taxList -- "+taxList);
return modelAndView;
}
}
I added taxList in launchconfirmCal() and trying to access the same in executeCalPay().
I tried to print taxList before adding to modelAttribute and the size is 12 and when I retireve in executeCalPay() it shows null.
I am not changing its value in JSP.

Remove or comment out this method and retry
#ModelAttribute("taxList")
public List<multiCalDto> getTaxList() {
return new ArrayList<multiCalDto>();
}
#ModelAttribute annotated methods are called before ALL request mapping methods, so it resets your taxList before the post method is called

Related

Return a ModelAndView with a List Object Simultaneously

I have a List of Managers that I need to return in my #Controller method. I also have a User form view that I need to return simultaneously. managerList is returned from a previous #Controller. I may have been staring at this screen to long, it may not even make sense to do so, but can this be done?
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
return new ModelAndView("userForm");
}
Output would be a blank user form with a List of managers that say would be loaded into a select input. Any ideas?
Thanks much
You can use use public ModelAndView(String viewName, Map<String, ?> model).In model you can put your list.
You can use the model map of the ModelAndView Object
try the below code
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}
#RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
#ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
model.addObject("managerList", managerList);
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}

Difference between two #RequestMapping annotations

I am pretty new in Spring MVC and I have the following doubt.
In a controller, I have a method annotated in this way:
#Controller
#RequestMapping(value = "/users")
public class UserController {
#RequestMapping(params = "register")
public String createForm(Model model) {
model.addAttribute("user", new Customer());
return "user/register";
}
}
So this method handle HTTP Request toward the URL /users?register where register is a parameter (because the entire class handle request toward /users resource).
Is it the same thing if, instead using the params = "register" I use the following syntaxt:
#Controller
public class UserController {
#RequestMapping("/users/{register}")
public String createForm(Model model) {
model.addAttribute("user", new Customer());
return "user/register";
}
}
I have deleted the mapping at class level and I use #RequestMapping("/users/{register}").
Is it the same meaning of the first example?
NO, they are completely different constructs:
Code 1
#Controller
#RequestMapping(value = "/users")
public class UserController {
#RequestMapping(params = "register")
public String createForm(Model model) {
model.addAttribute("user", new Customer());
return "user/register";
}
}
In this case, createForm method will be called when a HTTP request is made at URL /users?register. Quoting from Spring Javadoc, it means this method will be called whatever the value of the register HTTP parameter; it just has to be present.
"myParam" style expressions are also supported, with such parameters having to be present in the request (allowed to have any value).
Code 2
#Controller
public class UserController {
#RequestMapping("/users/{register}")
public String createForm(Model model) {
model.addAttribute("user", new Customer());
return "user/register";
}
}
In this case, #RequestMapping is declaring register as a PathVariable. The method createForm will be called if a HTTP request is made at URL /users/something, whatever the something. And you can actually retrieve this something like this:
#RequestMapping("/users/{register}")
public String createForm(#PathVariable("register") String register, Model model) {
// here "register" will have value "something".
model.addAttribute("user", new Customer());
return "user/register";
}

How to change url after spring controller invocation?

I have approximately following controllers:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public String foo(RedirectAttributes redirectAttributes, Model model) {
//logic
return bar(model);
}
#RequestMapping(value = "bar", method = RequestMethod.GET)
public String bar (Model model) {
model.addAttribute("value","magicValue")
return "myJsp";
}
my aim that after /foo invocation url was changed with bar
method bar shouldn't be broken.
Is it possible?
In method foo you can do:
return new ModelAndView("redirect:bar", modelName, model);
But this will add additional request to a server. It will also put the model values in the URL which is not always desirable

Post method is not executed when get method is invoked by ModelAndView from different method.

I need help.
Post method is not executed when get method is invoked by ModelAndView from different method.
Get method for mapping2 is correctly populated with data(taken from ModelAttribute), but
when I'm pressing form Submit button it is not executing post method for mapping2. It's running GET method all the time.
Url is mapping1 and it is not changing to mapping2, just jsp for mapping2 is displayed as content of mapping1.
Do you have any idea how to run post method?
The code from controller below.
#RequestMapping(value = "/mapping1", method = RequestMethod.POST)
public ModelAndView addItem(
#RequestParam(value = "year", required = true) final BigDecimal year,
#ModelAttribute("item") final Item item,
final HttpServletRequest request) {
ModelAndView mav = new ModelAndView("mapping2");
mav.addObject("item", item);
return mav;
}
#RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public Item addItemConfirmation(
#ModelAttribute("item") final Item item,
final HttpServletRequest request) {
return item;
}
#RequestMapping(value = "/mapping2", method = RequestMethod.POST)
public String addItemConfirmation(
#ModelAttribute("Item") final Item item,
final HttpServletRequest request) {
operations on item...
itemDAO.persist(item);
return "redirect:/itemAddSuccess
}

Spring MVC 3.0 Rest problem

I'm trying out Spring MVC 3.0 for the first time and like to make it RESTfull.
This is my controller:
#Controller
#RequestMapping(value = "/product")
#SessionAttributes("product")
public class ProductController {
#Autowired
private ProductService productService;
public void setProductValidator(ProductValidator productValidator, ProductService productService) {
this.productService = productService;
}
#RequestMapping(method = RequestMethod.GET)
public Product create() {
//model.addAttribute(new Product());
return new Product();
}
#RequestMapping(method = RequestMethod.POST)
public String create(#Valid Product product, BindingResult result) {
if (result.hasErrors()) {
return "product/create";
}
productService.add(product);
return "redirect:/product/show/" + product.getId();
}
#RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
public Product show(#PathVariable int id) {
Product product = productService.getProductWithID(id);
if (product == null) {
//throw new ResourceNotFoundException(id);
}
return product;
}
#RequestMapping(method = RequestMethod.GET)
public List<Product> list()
{
return productService.getProducts();
}
}
I have 2 questions about this.
I'm a believer in Convention over Configuration and therefor my views are in jsp/product/ folder and are called create.jsp , list.jsp and show.jsp this works relatively well until I add the #PathVariable attribute. When I hit root/product/show/1 I get the following error:
../jsp/product/show/1.jsp" not found how do I tell this method to use the show.jsp view ?
If I don't add the RequestMapping on class level my show method will be mapped to root/show instead of root/owner/show how do I solve this ? I'd like to avoid using the class level RequestMapping.
add your 'product' to Model and return a String /product/show instead of Product. In your show.jsp, you can access the product object form pageContext
Check out the section in the manual about "Supported handler method arguments and return types".
Basically, when your #RequestMapping method returns just an object, then Spring uses this as a single model attribute, and, I'm guessing, attempts to use the request URL as the basis for the view name.
The easiest way to return the view and data you want from the same method is probably to just have the method return a ModelAndView, so you can explicitly specify the viewName and the model data.

Categories