Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with BindingResults and Models with Composite View

I'm building my current website on top of a Composite View Template using Java, Spring MVC and Hibernate. Here's how it works:

Browser asks for a page.

An Internal Result View Class intercepts the request, gets its value and redirects to a Template jsp that merges a Header and a Footer with the page asked.

It then returns the template jsp as if it were the asked page.

The problem is that I'm having trouble at reading some information in the main page, as, for example, validation errors from the BindingResult object (see this unanswered question I made about the subject for details).

I think the request flow is working like this (correct me if wrong):

User Request Pages > InternalResourceView Intercepts and redirects to Template.jsp > A merged jsp is shown.

I think models from Requested Page are not present in the merged jsp.

Now I'm worried that when it comes to displaying lists and dynamic forms from data gathered in the database I might find troubles. Am I wrong in my troubles? If not, what can I do to fix problems such as the one with the BindingResult errors?

For Reference, use the link about BindingResult errors and the CompositeView Class below:

import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.InternalResourceView;

public class CompositeView extends InternalResourceView {

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String dispatcherPath = prepareForRendering(request, response);
        String[] parts = dispatcherPath.split("/");
        String endpath;
        if (parts[parts.length - 2].endsWith("views")) {
            endpath = parts[parts.length - 1];
        } else {
            endpath = parts[parts.length - 2] +"/"+ parts[parts.length - 1];

        }

        request.setAttribute("partial", endpath);


        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/template.jsp");
        rd.include(request, response);

    }
}

And Template.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="resources/js/jquery-3.1.0.js"></script>
<script type="text/javascript" src="resources/js/bootstrap.js"></script>

<link rel="stylesheet" href="resources/css/bootstrap.css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><spring:message code="application.title"></spring:message></title>
</head>
<body>
    <div class="row">
        <div class="col-md-8 col-md-offset-2">

            <jsp:include page="navbar/header.jsp" />
        </div>
    </div>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <jsp:include page="${partial}" />
            <jsp:include page="navbar/footer.jsp" />
        </div>
    </div>

</body>
</html>
like image 833
Tiago Duque Avatar asked Oct 19 '22 03:10

Tiago Duque


1 Answers

I think you are missing exposeModelAsRequestAttributes method

Expose the model objects in the given map as request attributes. Names will be taken from the model Map. This method is suitable for all resources reachable by RequestDispatcher.

 exposeModelAsRequestAttributes(model,request);

It should be placed at

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    //start :new code added by sanka
    exposeModelAsRequestAttributes(model,request);
    //end :new code added by sanka

    String dispatcherPath = prepareForRendering(request, response);

API

like image 106
Sanka Avatar answered Oct 21 '22 04:10

Sanka