2

I am trying to include a partial view in my jsp view page. How can i do that? i want to include my "addEmployeeContacts.jsp" to "addEmployee.jsp" page. addEmployee.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> Insert title here

Add Employee

Firstname: Lastname:

            <tr>
                <td>Date of Birth:</td>
                <td><form:input path="dob" type="date"/></td>
            </tr>
            <tr>
                <td colspan="2">
                <input type="submit" value="Add Employee">
                </td>
            </tr>
            </table>
        </form:form>
        <div>
        <jsp:include page="addEmployeeContacts.jsp">
         ${employeeContacts}
        </jsp:include>
        </div>
    </body>
    </html>
    </code>

And addEmployeeContacts.jsp

    <code>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
     <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>Add Employee</h1>
        <form:form commandName="employeeContacts">
            <table>
            <tr>
                <td>Contact Type</td>
                <td><form:input path="contactType"/></td>
            </tr>

            <tr>
                <td>Details</td>
                <td><form:input path="contactValue"/></td>
            </tr>

            <tr>
                <td colspan="2">
                <input type="submit" value="Add Contacts">
                </td>
            </tr>
            </table>
        </form:form>
    </body>
    </html>
    </code>

addEmployeeContactController

package com.employee.comtroller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.employee.model.Employee;
import com.employee.model.EmployeeContacts;
import com.employee.service.EmployeeContactsService;

@Controller
public class ContactsController {

    @Autowired
    private EmployeeContactsService employeeContactService;

    @RequestMapping(value="/addEmployeeContacts", method=RequestMethod.GET)
    public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,Model model){
        model.addAttribute(employeeContacts);
        return "addEmployeeContacts";

    }

    @RequestMapping(value="/addEmployeeContacts", method=RequestMethod.POST)
    public String addEmployeeContacts(@ModelAttribute("employeeContacts") EmployeeContacts employeeContacts,HttpSession session,BindingResult result){

        if(result.hasErrors()){
            System.out.println(result);
            return "addEmployeeContacts";
        }

        else{
            Employee employee = (Employee)session.getAttribute("employee");
            employeeContacts.setEmployee(employee);
            employeeContactService.save(employeeContacts);
        }

        return "redirect:index.jsp";
    }
}

Throwing error

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employeeContacts' available as request attribute

5
  • so whats the issue then? Commented Mar 8, 2015 at 9:24
  • Showing Error:org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'employeeContacts' available as request attribute Commented Mar 8, 2015 at 9:30
  • And what's the URL displayed in the address bar when you get this error? Commented Mar 8, 2015 at 9:44
  • Thanks for your reply localhost:8080/EmployeeManagement/addEmployee.html Commented Mar 8, 2015 at 9:46
  • Just wanted to find out if it helped Commented Mar 9, 2015 at 20:38

2 Answers 2

1

You need to use .tag as Main page and partial view as .jsp

for eg:

Create Layout.tag like this

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 <%@tag description="Overall Page template" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Home</title>
 </head>
 <body>
    <section class="content">
      <jsp:doBody />
    </section>
 </body>
</html>

Then Create a partial view like this

<%@taglib prefix="t" tagdir="/WEB-INF/tags/"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<t:Layout>    
   <div>
       your partial view html content
   </div>
</t:Layout>
Sign up to request clarification or add additional context in comments.

Comments

0

If your localhost:8080/EmployeeManagement/addEmployee.html directly lands you in addEmployee.jsp due to a mapping that i don't see in your post, then you need to do the following in your jsp. This should make a request to your controller to get the included view. Hope this helps.

<div>
        <jsp:include page="/addEmployeeContacts">
         ${employeeContacts}
        </jsp:include>
 </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.