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
Related
It was working fine but suddenly its now working for ${message} and others as ${param.someName}
this is my .java class
#PostMapping("/validate")
//All the libraries are also imported and no error in code
String validateLogin(String mail,String password,String location,HttpSession session,HttpServletRequest req,Model model) {
User user;
//user=urepo.findByEmail(mail);
user=urepo.findByEmailAndPassword(mail,password);
if(user!=null)
{
req.getSession();
session.setAttribute("userid", user);
System.out.println(location);
System.out.println(user.getFname());
return "redirect:/"+location;//+"?session=userid";
}
model.addAttribute("message", "please check the username/password");
//req.setAttribute("message", "Please check the username or password");
return "redirect:login?location="+location;
}
below is my .jsp login form when I'm entering wrong password on the jsp page I want to display the message which is written in the above java file but its not displaying the ${message} and also not working with the ${param.email} too but login is working fine
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
//Bootstrap link
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</head>
<body>
<div class="container-md text-center">
<div class="row mx-auto pt-5">
<h1>Welcome to Ekart...</h1>
<h3><i>Login please!!</i></h3>
<form action="validate" method="post">
<div class="col-md-5 col-sm mx-auto pt-2 pb-2">
<span>${message}</span>
UserName: <input type="email" name="mail" placeholder="email" value="${param.mail}">
</div>
<input type="hidden" name="location" value="${param.location}">
<!-- ${param.location} -->
<div>
<div class="col-md-5 col-sm mx-auto pt-2 pb-2">
Password: <input type="password" name="password" placeholder="password">
<br>
</div>
<input type="submit" class="btn btn-primary" value="login">
</form>
<br><br>
Create a Account?
</div>
</div>
</form>
</div>
</body>
</html>
Page "bookUpdate.html" does not work correctly because of the errors. I can open this page from page "bookList.html" via click to button "Edit". On all other pages bootstrap work correctly. I try to add to code something like "link rel="stylesheet" type="text/css" th:href="#{/webjars/bootstrap/css/bootstrap.min.css}" " but it did not work.
bookUpdate.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
layout:decorate="~{fragments/main_layout}">
<body>
<div layout:fragment="content" class="container mySpace">
<form th:action="#{/bookUpdate/__${book.id}__}" th:object="${book.id}"
method="post">
<div class="form-group">
<label for="topic" class="form-control-label">Topic</label> <input
type="text" class="form-control" th:field="${book.topic}"
id="topic" />
</div>
<div class="form-group">
<label for="description" class="form-control-label">Description</label>
<textarea class="form-control" th:field="${book.description}"
id="description" style="height: 95px"></textarea>
</div>
<div class="form-group">
<label for="link" class="form-control-label">Link</label> <input
type="text" class="form-control" th:field="${book.link}" id="link" />
</div>
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
</div>
</body>
</html>
SUGGESTION: Try changing "href" to "src":
https://stackoverflow.com/a/52435193/421195
ALSO: be sure to look at the other responses in the same thread.
I hope that helps ... and please post back what you find!
I resolved my issue. I added a links to bootstrap from file "main_layout.html", that I use for header, and added before them <base href="/">. Now it is working fine.
<head>
<base href="/">
<link rel="stylesheet" href="../../static/css/style.css"
th:href="#{css/style.css}" />
<link rel="stylesheet" href="../../static/css/materia/bootstrap.min.css"
th:href="#{css/materia/bootstrap.min.css}" />
<script type="text/javascript" th:src="#{scripts/jquery-3.2.1.min.js}"></script>
</head>
I have error in first line of following jsp page
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%#taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-
1">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/boo
tstrap.min.css">
<title>Products</title>
</head>
<body>
<section>
<div class="jumbotron">
<div class="container">
<h1>Products</h1>
<p>Add products</p>
</div>
</div>
</section>
<section class="container">
<form:form modelAttribute="newProduct" class="form-horizontal">
<fieldset>
<legend>Add new product</legend>
<div class="form-group">
<label class="control-label col-lg-2 col-lg-2" for="productId">Product
Id</label>
<div class="col-lg-10">
<form:input id="productId" path="productId" type="text"
class="form:input-large" />
</div>
</div>
<!-- Similarly bind <form:input> tag for
name,unitPrice,manufacturer,category,unitsInStock and unitsInOrder
fields-->
<div class="form-group">
<label class="control-label col-lg-2" for="description">Description</label>
<div class="col-lg-10">form:textarea id="description"
path="description" rows = "2"/></div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="discontinued">Discontinued</label>
<div class="col-lg-10">
<form:checkbox id="discontinued" path="discontinued" />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="condition">Condition</label>
<div class="col-lg-10">
<form:radiobutton path="condition" value="New" />
New
<form:radiobutton path="condition" value="Old" />
Old
<form:radiobutton path="condition" value="Refurbished" />
Refurbished
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" id="btnAdd" class="btn btn-primary"
value="Add" />
</div>
</div>
</fieldset>
</form:form>
</section>
</body>
</html>
while doing this jsp i got error, I am new to view part and have little to none knowledge of Jstl el but while learning spring I got stuck in this error which says
Multiple annotations found at this line:
> - Missing end tag for
> "form:input"
>- Missing end tag for
> "form:input"
Am I missing something, how to solve this? even a small hint is welcomed too
Thanks
el will also evaluate expressions in comments, so you have to make the tags in comments as it is in "normal" page
Change
<!-- Similarly bind <form:input> tag for
name,unitPrice,manufacturer,category,unitsInStock and unitsInOrder
fields-->
to
<!-- Similarly bind <form:input/> tag for
name,unitPrice,manufacturer,category,unitsInStock and unitsInOrder
fields-->
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">
As my Spring MVC web app is using
Servlet version: 3.0
JSP version: 2.1
Java version: 1.7.0_11
and Had deployed war on OpenShift Tomcat 7 (JBossEWS 2.0 ) using above version ,it always giving me error :
org.apache.jasper.JasperException: Unable to compile class for JSP
java.util.NoSuchElementException
java.util.ArrayList$Itr.next(ArrayList.java:834)
and i tried using version JSP 2.0 and Servlet 2.5..still no use.
Any Help?
Login.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<section id="page">
<!-- HEADER -->
<header>
<!-- NAV-BAR -->
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div id="logo">
</div>
</div>
</div>
</div>
<!--/NAV-BAR -->
</header>
<!--/HEADER -->
<!-- LOGIN -->
<section id="login" class="visible">
<div class="container">
<div class="row">
<c:if test="${not empty error}">
<div class="errorblock" style="text-align:center;">
<b style="text-align:center;">Your login attempt was not successful, try again.</b>
</div>
</c:if>
<div class="col-md-4 col-md-offset-4">
<div class="login-box-plain divide-40">
<form:form method="post" action="authenticate" modelAttribute="userInfo">
<div class="form-group">
<label for="exampleInputEmail1">User Name</label>
<i class="fa fa-user"></i>
<form:input path="loginID" id="username" placeholder="" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<i class="fa fa-lock"></i>
<form:password class="form-control" path="password" id="password" placeholder=""/>
</div>
<div>
<button type="submit" class="btn btn-danger">Login</button>
</div>
</form:form>
</div>
</div>
</div>
</div>
</section>
<!--/LOGIN -->
</section>
You can find the version of the libraries that tomcat 7 is using by sshing into your application and running the following command:
ls ~/jbossews/lib
Which will output the following:
ls ~/jbossews/lib/
annotations-api-7.0.54.jar
el-api-7.0.54.jar
servlet-api.jar
tomcat-i18n-ja-7.0.54.jar
annotations-api.jar
el-api.jar
tomcat7-websocket-7.0.54.jar
tomcat-i18n-ja.jar
catalina-7.0.54.jar
extras
tomcat7-websocket.jar
tomcat-jdbc-7.0.54.jar
catalina-ant-7.0.54.jar
jasper-7.0.54.jar
tomcat-api-7.0.54.jar
tomcat-jdbc.jar
catalina-ant.jar
jasper-el-7.0.54.jar
tomcat-api.jar
tomcat-util-7.0.54.jar
catalina.jar
jasper-el.jar
tomcat-coyote-7.0.54.jar
tomcat-util.jar
commons-collections-eap6.jar
jasper.jar
tomcat-coyote.jar
websocket-api-7.0.54.jar
commons-dbcp-eap6.jar
jasper-jdt.jar
tomcat-i18n-es-7.0.54.jar
websocket-api.jar
commons-loggin
g-adapters-eap6.jar
jsp-api-7.0.54.jar
tomcat-i18n-es.jar
commons-logging-api-eap6.jar
jsp-api.jar
tomcat-i18n-fr-7.0.54.jar
commons-pool-eap6.jar
servlet-api-7.0.54.jar
tomcat-i18n-fr.jar