How to get variables from one Spring MVC Controller to Another - java

I m novice to spring mvc. Could you please help me to design below.
I have newly written Controller A which is bind with model attribute “attribA” And new jsp is customerInformation.
Now I have existing Controller B which is bind with model attribute “attribB” and jsp is existing customerSummary which I will modify as per requirement where I want to display contents set by the user on previous jsp in model attribA.
How can I pass content or attributeA from new Controller A in an existing controller Controller B to show on second jsp customerSummary
Thank You

you can do this by using "RedirectAttributes"
#PostMapping("/userWiseNewMenuPermission")
public ModelAndView setUserWiseNewMenuPermission(ModelAndView modelAndView,RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("comfmMassg","Menu Permission Save Successful!");
modelAndView.setViewName("redirect:/userList?page=1");
return modelAndView;
}
in this Post controller redirect to a get controller with a attribute name "comfmMassg" ......
#GetMapping("/userList")
public ModelAndView getMenu(ModelAndView modelAndView,#ModelAttribute("comfmMassg") String masg) {
modelAndView.addObject("masg", masg);
modelAndView.setViewName("userList");
return modelAndView;
}
this example work in spring boot ............

Related

Design to fetch DB data and load in jsp while application starting

We just started our development using jsp's and spring mvc framework. Looking for a good approach to load data from DB and save all the jsp page data into DB. Here is the brief description:
As there is no login details yet decided, When I start my application, this loads my jsp page which we are working on. This jsp page contains 4 to 5 sections. Each and every section requires some preloaded data which should come from DB.
We need to load and display this data while loading the jsp page itself.
Once the jsp page is loaded with all the pre-populated values from DB, there will be other fields where user enter some data and finally Save this jsp page.
I have around 8 to 9 drop-down lists in jsp which should get loaded from DB. How I can load all this data while loading the page ? Can I load all this data once from db and put it as part of one object and display as part of jsp ?
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView visitHome(HttpServletRequest request,
HttpServletResponse response) throws IOException {
System.out.println("Testing purpose");
return new ModelAndView("first");
}
As we are using Spring MVC framework here, I am redirecting from jsp to controller as above, and then to service then to DAO then to DB. How I can include all the DB loading logic as part of above method ?
Please suggest. Thanks in advance.
You can create some pojos and return pojos with model and view. Assume that you have class SectionData and you want to set some data to it and return it with view.
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView visitHome(HttpServletRequest request,
HttpServletResponse response) throws IOException {
SectionData sectionData = new SectionData();
sectionData.setName("John Citizen");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("firstSectionData", sectionData);
modelAndView.setViewName("first");
return modelAndView;
}
And then you can bind jsp elements with section data such as
My name is ${firstSectionData.name}
Now the HTML page will show "My name is John Citizen".

Can I configure both HTML and Spring Data REST JSON on the same URI?

I am writing a web application with Spring MVC.
Keeping with REST principles, I am wanting to use consistent URIs in my application for the same resources and to use content negotation to select whether to return HTML or JSON. Visiting /people/bob in a browser should show his profile page, and getting it with curl should give me a JSON representation.
However, because of a semantic limitation (bug?) in the RequestMappingHandlerMapping, I can't "fall through" to the generic Spring Data REST mappings if I define any explicit controller, such as an HTML controller, on a matching URI. Besides manually implementing a #RestRepositoryController for every matching HTML controller, is there any other simple way to make Spring MVC content-negotiate between HTML and JSON mappings?
Example mapping that doesn't work:
#BasePathAwareController
class PersonHtmlController {
#GetMapping(path = '/people/{id}', produces = 'text/html')
ModelAndView person(#PathVariable Person id) {
new ModelAndView('person', [person: id])
}
}
This produces the expected HTML output but returns a 406 Not Acceptable when I ask for JSON.
You can go for contentnegotiatingviewresolver. Following is an example.
#RequestMapping(value = "/users", method = RequestMethod.GET)
public ModelAndView userDetails() {
ModelAndView modelAndView = new ModelAndView();
List userDetails = userService.getUserDetails();
modelAndView.addObject("users", userDetails);
modelAndView.setViewName("userDetails");
return modelAndView;
}
So whenever a request comes with /users then html page will be served and when the request comes as /users.json then a JSON response will be generated.
You can configure it in following way by overriding following method by extending WebMvcConfigurerAdapter provided by Spring.
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp").viewClass(JstlView.class);
registry.enableContentNegotiation(
new MappingJackson2JsonView()
);
}
Hope this helps.
Reference:
contentnegotiating view resolver example

Spring MVC parent template model component

I'm using Spring MVC 4 and I'm building a site with a template that requires several common components across pages, such as login status, cart status, etc. An example of controller function would be this:
#RequestMapping( path = {"/"}, method=RequestMethod.GET)
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("listProducts", products );
mav.addObject("listCategories", menuCategoriasUtils.obtainCategories());
return mav;
}
What would be a good way/pattern to feed these elements that do not belong to the controller we are currently calling so we don't repeat over and over unrelated operations in every method of every controller?
Thanks!
There is a several approaches to show common data in views. One of them is using of #ModelAttributte annotation.
Lets say, you have user login, that needed to be shown on every page. Also, you have security service, where from you will get security information about current login. You have to create parent class for all controllers, that will add common information.
public class CommonController{
#Autowired
private SecurityService securityService;
#ModelAttribute
public void addSecurityAttributes(Model model){
User user = securityService.getCurrentUser();
model.addAttribute("currentLogin", user.getLogin());
//... add other attributes you need to show
}
}
Note, that you don't need to mark CommonController with #Controller annotation. Because you'll never use it as controller directly. Other controllers have to be inherited from CommonController:
#Controller
public class ProductController extends CommonController{
//... controller methods
}
Now you should do nothing to add currentLogin to model attributes. It will be added to every model automatically. And you can access user login in views:
...
<body>
<span>Current login: ${currentLogin}</span>
</body>
More details about usage of #ModelAttribute annotation you can find here in documentation.

Spring MVC - Session Attributes - Should that be passed in all controllers as ModelAttribute

Am #SessionAttributes for maintaining SpringMVC.
Say, #SessionAttribute("user")
Currently am passing the object as ModelAttribute in all the controller, which needs to use the SessionObject "user" like
Class controller{
public ModelAndView method1(#ModelAttribute("user")){ }
public ModelAndView method2(#ModelAttribute("user")){ }
public ModelAndView method3(#ModelAttribute("user")){ }
public ModelAndView method4(#ModelAttribute("user")){ }
}
Is this the only way??
or
Is there any other way? such that I can have a base controller, which can return the session object by just extending the the base controller.
What I've been using in some of my projects is this:
String user = (String) hsr.getSession().getAttribute("user");
If you're looking for some sort of authentication I suggest you start using spring security or other authentication mechanisms that can filter out pages according to roles or authentication status.
Not sure what your exact requirement is, but what about creating a filter/interceptor that reads the value from session and stores it in a ThreadLocal that can be accessed by controllers later
The controllers that need to access #SessionAttributes you need to add the annotation as shown below.
#Controller
#SessionAttributes({"user"})
public class Controller {
.............
}
HTH

spring mvc: don't redirect after login - need two models?

I coded a wee login controller. It has an onSubmit method which logs in the user. If the login is successful I want to show the front page without having to redirect. The front page needs content from some other model. Because my LoginController already has a LoginModel it can't also have the InformationModel.
Is there some way to get a pointer on the InformationModel? Or some call to get the ModelAndView of the InformationController? That controller provides a handleRequest method.
I think this is more a fundamental question, but if you need code to answer it I will supply it.
I',m not sure if I get your question correctly, but
a.) You can add multiple models on your ModelAndView object. Use:
modelAndView.addObject("informationModel", informationModelObject);
b.) If successful login, set the view to your front page view:
modelAndView.setView("frontPageView");
To access your InformationController on your LoginController, you can autowire it =)
#Autowired
InformationController informationController;
#RequestMapping( ... ) // assuming you define it here
public ModelAndView onSubmit(... ) {
// .. code here
if (loginsuccess) {
InformationModel informationModelObject = informationController.handleRequest(...);
modelAndView.addObject("informationModel", informationModelObject);
modelAndView.setView("frontPageView");
}
else {
modelAndView.setView("loginFailView");
}
return modelAndView;
}

Categories