4

I am posting a form to SPRING MVC Controller and then after processing the data over there I am redirecting to the same page by doing something like this

@RequestMapping(value = "/{locale}/pegasus", method = RequestMethod.GET)
public String pegasus(ModelMap model) {
    RestTemplate rest = new RestTemplate();
    String response=rest.getForEntity("http://url.co.uk/pegasus/", String.class).getBody();
    model.addAttribute("firstName", response);
    model.addAttribute("email", response);
    return "/Product/Page/Sandbox";
}

The Post Operation is working fine but the problem is that when SPRING MVC Controller redirecs the pageModel Bean and markup bean are not in scope

 <jsp:useBean id="pageModel" type="com.sdl.webapp.common.api.model.PageModel" scope="request"/> 
<jsp:useBean id="markup" type="com.sdl.webapp.common.markup.Markup" scope="request"/>

I am getting this exception

        Type Exception Report

Message javax.servlet.ServletException: java.lang.InstantiationException: bean pageModel not found within scope

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.InstantiationException: bean pageModel not found within scope
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:565)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    com.nevado.cookie.SSOFilter.doFilterInternal(SSOFilter.java:30)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Root Cause

javax.servlet.ServletException: java.lang.InstantiationException: bean pageModel not found within scope
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
    org.apache.jsp.WEB_002dINF.Views.Product.Page.PegasusSandbox_jsp._jspService(PegasusSandbox_jsp.java:436)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    com.nevado.cookie.SSOFilter.doFilterInternal(SSOFilter.java:30)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Root Cause

java.lang.InstantiationException: bean pageModel not found within scope
    org.apache.jsp.WEB_002dINF.Views.Product.Page.PegasusSandbox_jsp._jspService(PegasusSandbox_jsp.java:170)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    com.nevado.cookie.SSOFilter.doFilterInternal(SSOFilter.java:30)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

Can someone please tell me How do I populate those beans as they are needed to display Page title etc.

4

By returning a view name you tell Spring MVC to find this view in web resources and then to invoke the code in that view. If your view contains jsp:useBean tag, it obviously look for a PageModel bean in the requested scope. I don't see where you set the requested bean in your controller. Default DXA controllers do it.

Then the main question if whether you need those beans in a request and in your view. If yes, you have to provide them (like e.g. in the PageController). If not, then remove its usage from the view.

If you decide to use DXA implementations (currently you basically implement your own webapp without actual usage of DXA), then consider either subclass PageController (look at SearchController for example), or write your ModelBuilder to add some logic to model building.

1

Found a workaround but not the correct answer

So I was using this code to display page

   @RequestMapping(value = "/{locale}/pegasus", method = RequestMethod.GET)
public String pegasus(ModelMap model) {
    RestTemplate rest = new RestTemplate();
    String response=rest.getForEntity("http://url.co.uk/pegasus/", String.class).getBody();
    model.addAttribute("firstName", response);
    model.addAttribute("email", response);
    return "/Product/Page/Sandbox";
}

But then I found the workaround by looking at DXA-AudienceManager example

It seems that rather than giving a conventional page name from Spring Controller we will need to do a redirect so that the beans like "pagModel" and "markup" persist the Request scope

 <jsp:useBean id="pageModel" type="com.sdl.webapp.common.api.model.PageModel" scope="request"/> 
<jsp:useBean id="markup" type="com.sdl.webapp.common.markup.Markup" scope="request"/>
<jsp:useBean id="user" class="com.nevado.model.UserForm" scope="request"/>

So I had to do following in my controller class, redirect the page and add flash attributes to hold the value. Also, note that since I was using this 'webRequestContext' to get the URL. The Page name in the last line doesn't include the directory structure as in earlier code.

@Autowired
   public UserFormController(  WebRequestContext webRequestContext) {
       this.webRequestContext = webRequestContext;
   }

  @RequestMapping(value = "{locale}/pegasus", method = RequestMethod.GET)
public String pegasus(ModelMap model, RedirectAttributes redirectAttributes) {
    RestTemplate rest = new RestTemplate();
    String response=rest.getForEntity("http://url.co.uk/pegasus/", String.class).getBody();
    redirectAttributes.addFlashAttribute("pegasusResponse", response);
    return REDIRECT_PREFIX + webRequestContext.getLocalization().getPath() +"/Sandbox";
}

However this is a workaround, I would still like to get my things working without the need of a Redirect and in traditional Spring MVC way by just providing the view-name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.