How to render partial view in Spring MVC - spring

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

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>

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>

Related

Spring MVC serve static file when submit a form

I'm running through weird behavior in Spring MVC. I try to make custom login page with spring security.
Here is my simple login page with a link to a style sheet login.jsp:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<link href="/resources/css/style.css" rel="stylesheet"/>
</head>
<body>
<h1>Login Form</h1>
<form:form action="/authuser" method="post">
<c:if test="${param.logout != null}">
<i>You have been logout!</i>
</c:if>
<c:if test="${param.error != null}">
<i>Invalid username or password!</i>
</c:if>
<p>
User name: <input type="text" name="username"/>
</p>
<p>
Password: <input type="password" name="password"/>
</p>
<input type="submit" value="Login"/>
</form:form>
</body>
</html>
when I fill out the form and hit submit it redirect me to http://localhost:8080/resources/css/test.css and serves me with stylesheet file!
when I remove the stylesheet link, it works fine and redirects me to the home page.

Proper installation of JSTL for Maven (Intellij IDEA)

I am trying to use JSTL for my index.jsp page, but for some reason each time after packaging project to .war and running with Tomcat it gives me following errors:
HTTP Status 500 - /index.jsp (line: 12, column: 0) Unterminated <c:if tag
or
HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
From what I found on google, there are 2 ways to install JSTL into your Maven project:
1) Add this to pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2) Add some jars to WEB-INF/lib, but here's the problem: no such folder was automaticaly created and if I do it manually it does not help. Project structure looks like this:
The code of index.jsp is the following:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
So, what should I do to make those work? I can't find any guide or piece of information that could help me solving this. Thanks for having a look at my problem!
Just what compiler tells you: Unterminated c:if tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
Look at line 4 of your jsp:
<title><c:if> Tag Example</title>
It must be
<title><c:if> Tag Example </c:if></title>
EDIT: As seems that I explained badly, I have tested in a running web-app, and this way (that is just what I was trying to explain) works:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>

request.contextPath not resolved inside Apache Tiles JSP fragments

I have a Spring + Apache Tiles 3 Aplication.
I have a layout and inside of this, the header, the body and the footer:
defaultLayout.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title" /></title>
<link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
<header id="header">
Web Application Context Path="${pageContext.request.contextPath}"
<tiles:insertAttribute name="header" />
</header>
<section id="sidemenu">
<tiles:insertAttribute name="menu" />
</section>
<section id="site-content">
<tiles:insertAttribute name="body" />
</section>
<footer id="footer">
<tiles:insertAttribute name="footer" />
</footer>
</body>
</html>
The "${pageContext.request.contextPath}" works fine inside the Layout, but not on the JSP children's:
menu.jsp
<nav>
<img class="logo" src="${pageContext.request.contextPath}/static/img/Linux-icon.png">
<ul id="menu">
<li>Home</li>
<li>Products</li>
<li>Contact Us</li>
</ul>
This prints the "${pageContext.request.contextPath}" "as is" in the result HTML.
Try to use <%# page isELIgnored="false" %>. It will solve your problem
Since you mentioned HTML code and say that the context path is printed "as-is", is this code in HTML file? Then it will not work.
Check this question - ${pageContext.request.contextPath} is not working on plain HTML

unable to pass variables between jsp templates

I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.
a.jsp
start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
stop including
b.jsp
${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>
output
start including
null
stop including
You have to set your param before include.
start including
<jsp:params>
<jsp:param name="somevar" value="zzzz" />
</jsp:params>
<jsp:include page="b.jsp">
</jsp:include>
stop including
And can u try this in b.jsp:
<%=request.getParameter("somevar");%>
What is output?
Try the following code:
home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<jsp:include page="b.jsp">
<jsp:param name="something" value="something" />
</jsp:include>
<h1>Bye World</h1>
</body>
</html>
In the b.jsp page use the following code.
<%# 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"%>
<!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>JSP Param example</title>
</head>
<body>
<h1>
<c:out value="${param.something}" />
</h1>
</body>
</html>
The output must be
Hello world!
something
Bye World
Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?

Struts2 ajax theme problem in IE9

We have an running application that is developed in struts-2.0.14. In this we used Ajax theme for displaying contents.
This ajax theme is running properly in all browsers except IE9 browsers, In IE9 this ajax theme making problem . The result is showing in new tab instead of showing in target element(div) specified by targets property.
Parent Page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<s:head theme="ajax" debug="false" />
</head>
<body>
<s:url id="changePwd" action="changePassword" />
<s:div theme="ajax" id="pwdDiv" executeScripts="true" href="%{changePwd}" loadingText="Loading..."/>
</body>
</html>
Inner page
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<s:form action="changePassword" id="resetPassword" name="resetPassword">
<s:textfield name="username" id="username" />
<s:password showPassword="true" name="newpassword" id="newpassword" value="%{newpassword}"/>
<s:password showPassword="true" name="confirmpassword" id="confirmpassword" value="%{confirmpassword}"/>
<s:submit value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />
</s:form>
When Submitting the form in Inner Page the resulting page is popup into new tab. actually it should replace the content of pwdDiv.
Note: the same working properly in other browsers including (IE7,8)
Update:
making showLoadingText as true making the request as Ajax but the values are passed as null (password,confirm password ect,. -all fields)
Adding type="button" in s:submit button solve the problem. Don't know what is behind this !
<s:submit **type="button"** value="Confirm" showLoadingText="false" onclick="clearMsg();" theme="ajax" targets="pwdDiv" executeScripts="true" cssClass="userbutton" />

Resources