Form validation errors inside form spring mvc - java

I am trying to print form validation next to each field. My jsp file looks like
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
.error {
color: red; font-weight: bold;
}
</style>
<title>
FORM
</title>
</head>
<body>
<div id="wrap" >
<div class= "container" style="min-height:400px;">
<hr>
<form:errors path="applicant.applicant_email" cssClass="error"/>
<form:form method = "post" action = "${pageContext.request.contextPath}/addApplicant" modelAttribute = "applicant">
<form:errors path="applicant.applicant_email" cssClass="error"/>
<div class="form-group col-lg-4 col-md-4 col-sm-6">
<label>Email</label>
<input name = "applicant_email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="form-group col-lg-4 col-md-4 col-sm-6 ">
<input type = "submit" />
</div>
</form:form>
</div>
</div>
<!-- div Wrap ka End -->
<hr>
<br>
<br>
</body>
</html>
"Applicant" is the name of my model class.
Now it prints fine outside the form, but prints nothing if form:errors is used inside form:form tag. What am I missing. Any help is highly appreciated. Thank you.

I figured put what the problem was. All had to do was add path attributes to the input fields. Like in my example:
<input name = "applicant_email" class="form-control" id="inputEmail" placeholder="Email" path = "applicant_email">

Related

spring form tag radiobutton select one of group

I have one question about spring tag form and radiobuttons. I have a task something like testing. User logged in and can choose some test. On the test page i'm using spring tag form and radiobuttons for answering questions. For one question 4 answers. But on the test page when i'm trying to select one answer for question one is selected for all question. I mean i can select only one radio button. I know that in html for create group of radio buttons they should have same names. How i can create a group of radio buttons with spring tag form?
Here test_page.jsp
<%# page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="/resources/js/timer.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="/resources/styles/style.css"/>
<title>Test page</title>
</head>
<body onload="startTimer()">
<div id="info">
<h3>
Subject: <c:out value="${test.subject}"/>
</h3>
<h3>
Number of questions:
<c:out value="${test.questionNumber}"/>
</h3>
<h3>
Difficulty:
<c:out value="${test.difficulty}"/>
</h3>
<div id="clock">
<p>
<label for="timer">Time for test</label>
<span id="timer" style="color: #4af; font-size: 150%; font-weight: bold;">
<c:out value="${test.timer}"/></span>
</p>
</div>
</div>
<div id="test">
<form:form action="result" commandName="result" method="post">
<form:input type="hidden" path="testSubject" value="${test.subject}"/>
<form:input type="hidden" path="difficulty" value="${test.difficulty}"/>
<c:forEach items="${test.questionAnswersMap}" var="entry">
<h4><c:out value="${entry.key}"/></h4>
<div class="form-check">
<ul style="list-style: none">
<form:radiobuttons path="answers" items="${entry.value.existingAnswers}" element="li" class="form-check-input"/>
</ul>
</div>
</c:forEach>
<button id="finish" type="submit" class="btn btn-primary btn-block">Finish</button>
</form:form>
</div>
</body>
</html>
I'm using spring 4.
If someone knows how to solve that i will be grateful.

How to link bootstrap files with jsp file

Okay, I'm new to JSP and I'm trying to create a simple form with button in a JSP file index.jsp and link it to bootstrap. I just can't figure out how to call the bootstrap files from JSP. Here is my JSP code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<link href=”Bootstrap/css/bootstrap.min.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”Bootstrap/js/bootstrap.min.js”></script>
<title>Bootstrap Project</title>
</head>
<body>
<div class=”container”>
<form role=”form”>
<div class=”form-group”>
<label for=”exampleInputEmail1″>Email address</label>
<input type=”email” class=”form-control” id=”exampleInputEmail1″ placeholder=”Enter email”>
</div>
<div class=”form-group”>
<label for=”exampleInputPassword1″>Password</label>
<input type=”password” class=”form-control” id=”exampleInputPassword1″ placeholder=”Password”>
</div>
<div class=”form-group”>
<label for=”exampleInputFile”>File input</label>
<input type=”file” id=”exampleInputFile”>
<p class=”help-block”>Example block-level help text here.</p>
</div>
<div class=”checkbox”>
<label>
<input type=”checkbox”> Check me out
</label>
</div>
<button type=”submit” class=”btn btn-default”>Submit</button>
</form>
</div>
</body>
</html>
And here is my folder arrangement:
I'm using Eclipse IDE and Tomcat as the server. Where am I going wrong?
Thanks.

cannot load a nested template .jsp file

I am trying to make a basic template system for my Spring MVC project. I am struggling to have a dynamic navigation bar and dynamic content on the same page. When I do not have the #RequestMapping(value = "/generic/navbar"), I get the expected output of "loading navbar". However, all the Bootstrap CSS and JQuery is broken. Inspecting the resources in chromes shows that the Bootstrap and JQuery scripts have been replaced with the HTML code in navbar.jsp. If I keep the #RequestMapping(value = "/generic/navbar"), the page loads the javascript correctly, but the navbar is not loaded.
I suspect that I have messed up something pertaining to the Request, but I do not know how to fix it. Can anyone help me please?
This is my code that will load the navigation bar.
#Controller
#RequestMapping(value = "/generic/navbar") //causes problems
public class NavbarController {
#RequestMapping(method = {RequestMethod.GET})
public String renderNavbar(Map<String, Object> model) {
System.out.println("loading navbar");
System.out.println(model);
return "generic/navbar";
}
}
This is the associated navbar.jsp file:
<%# page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<div id="navbar" class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index">IPIMS</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li> Home </li>
</ul>
</div>
</div>
this is a page where I try to use two jsp files:
<%# page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%#taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="input" uri="http://www.springframework.org/tags/form" %>
<t:userpage>
<jsp:attribute name="nav">
<jsp:include page="generic/navbar.jsp"/>
</jsp:attribute>
<jsp:attribute name="footer">
<jsp:include page="generic/footer.jsp"/>
</jsp:attribute>
<jsp:body>
HTML in here...
</jsp:body>
</t:userpage>
and this is my custom tag:
<%#tag description="Overall Page template" pageEncoding="UTF-8"%>
<%#attribute name="nav" fragment="true" %>
<%#attribute name="footer" fragment="true" %>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='/webjars/bootstrap/3.3.5/css/bootstrap.min.css'>
<script type="text/javascript" src="/webjars/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div id="nav">
<jsp:invoke fragment="nav"/>
</div>
<div id="body">
<jsp:doBody/>
</div>
<div id="pagefooter">
<jsp:invoke fragment="footer"/>
</div>
</body>
</html>

JSP exception quote symbol expected

I am new to spring MVC, JSP and bootstrap. I am trying to create a sign in form and read the values from it. For this purpose the login.jsp file I wrote is;
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%#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>Bootstrap Form With Spring Mvc Example</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css"
rel="stylesheet">
<link href="https://code.jquery.com/jquery-2.1.3.js" rel="stylesheet">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="container">
<form:form class="form-signin" method="POST" commandName="user">
<h2 class="form-signin-heading">
<spring:message code="main.signin" />
</h2>
<label for="email" class="sr-only">
<spring:message code="main.email" />
</label>
<form:input type="email" path="email" id="email" class="form-control" placeholder=<spring:message code="main.email"/> required autofocus/>
<label for="password" class="sr-only">
<spring:message code="main.password" />
</label>
<form:input type="password" path="password" id="password" class="form-control" placeholder=<spring:message code="main.password"/> required/>
<div class="checkbox">
<label> <input type="checkbox" id="rememberme">
<spring:message code="main.rememberMe" />
</label>
</div>
<input class="btn btn-lg btn-primary btn-block" type="submit" id="submit" value=<spring:message code="main.signinBtn" /> >
</form:form>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
But I get
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/SpringMVC] threw exception [/WEB-INF/pages/login.jsp (line: 26, column: 86) quote symbol expected] with root cause
org.apache.jasper.JasperException: /WEB-INF/pages/login.jsp (line: 26, column: 86) quote symbol expected
exception. The line I get this exception is
<form:input type="email" path="email" id="email" class="form-control" placeholder=<spring:message code="main.email"/> required autofocus/>
I don't have any idea about the problem. Can you help?
You can't use <spring:message> within <form:input> in the manner attempted. Look at this question for the solution: how-to-use-springmessage-inside-an-attribute-of-forminput

The resource required (servlet) not available

I have the following problem when trying to use the login.jsp
I have the following code in the login
<%# include file="/jsp/include.jsp"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function sendForm() {
document.formLogin.submit();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example :: Spring Application</title>
</head>
<body>
<div class="container">
<form class="bs-docs-example form-horizontal"
action="ServletValidation" name="formLogin" id="formLogin"
method="post">
<legend>Login</legend>
<div class="control-group">
<label for="inputUsername" class="control-label">Email</label>
<div class="controls">
<input type="text" id="inputUsername">
</div>
</div>
<div class="control-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="controls">
<input type="password" id="inputPassword">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox"> <input type="checkbox">
Remember me
</label>
<button class="btn" type="submit" action="sendForm();">Sign
in</button>
</div>
</div>
</form>
</div>
</body>
</html>
And the following text in the web.xml
<servlet>
<description></description>
<display-name>ValidationServlet</display-name>
<servlet-name>ValidationServlet</servlet-name>
<servlet-class>bt.servlet.ValidationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidationServlet</servlet-name>
<url-pattern>/ValidationServlet</url-pattern>
</servlet-mapping>
But once I click the button for submit it returns:
State HTTP 404 - /bt/jsp/ServletValidation
Description The resource required (/bt/jsp/ServletValidation) is not
available.
The folder structure is the following:
+bt
-src
-WebContent
-jsp
-resources
-WEB-INF
-classes
*web.xml
*index.jsp
The problem I'm finding is why is it sending to that URL
There are two problems:
Your servlet maps to the URL /ValidationServlet and your form has the action set to ServletValidation.
Maybe your login.jsp isn't at the same level of your servlet mapping.
The best solution would be setting the action of your form to map the full URL that maps to your servlet. This can be achieved using Request#getContextPath():
<form action="${request.contextPath}/ValidationServlet" ...>
<!-- content... -->
</form>
If you don't use JSTL in your project, then do it. You should avoid scriptlets in your jsp (those <% ... %> tags that hold nasty Java code inside the JSP). But if you don't, then you should try the following:
<form action="<%=request.getContextPath()%>/ValidationServlet" ...>
<!-- content... -->
</form>
Still, the first way is the best to go.
More info:
How to avoid Java Code in JSP-Files?
Our JSTL wiki page
Just change the web.xml file
<url-pattern>/ServletValidation</url-pattern>
ServletValidation is different from ValidationServlet.

Categories