Spring MVC with Spring Boot: form tag error - equal symbol expected - spring

I am trying to fill a form with values provided by the controller using tags in an app built with spring boot. But I get an error:
org.apache.jasper.JasperException: /WEB-INF/jsp/index.jsp (line: 52, column: 102) equal symbol expected
related part of index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title><spring:message code="title.main" /></title>
</head>
<body>
<%#include file="include/navbar.jsp" %>
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-sm-offset-0 col-md-8 col-md-offset-2 main">
<div class="panel panel-default">
<div class="panel-body">
<form:form method="get" id="indexForm" commandName="indexForm" action="<c:url value="index_search" />" role="form">
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="city_out"><spring:message code="label.index.0" /></label>
<input:input type="text" class="form-control" id="city_out" path="city_out" />
</div>
<div class="form-group">
<label for="city_in"><spring:message code="label.index.1" /></label>
<form:input type="text" class="form-control" id="city_in" path="city_in" />
</div>
<div class="form-group">
<label for="company"><spring:message code="label.index.2" /></label>
<form:input type="text" class="form-control" id="company" path="company" />
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label for="date0"><spring:message code="label.index.3" /></label>
<form:input type="datetime" class="form-control" placeholder="dd.MM.yyyy [HH:mm]" id="date0" path="date0" />
</div>
<div class="form-group">
<label for="date1"><spring:message code="label.index.4" /></label>
<form:input type="text" class="form-control" placeholder="dd.MM.yyyy [HH:mm]" id="date1" path="date1" />
</div>
<div class="form-group" align="right">
<br />
<button type="submit" id="search_btn" data-loading-text="Loading..." class="btn btn-primary">Search</button>
</div>
</div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I have created a class to match form fields:
public class IndexForm {
private String city_out;
private String city_in;
private String company;
private String date0;
private String date1;
// getters, setters
}
And created a bean in main class (I hope so):
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class Application extends SpringBootServletInitializer {
#Bean
public static IndexForm indexForm() {
return new IndexForm();
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Controller contains this method:
#RequestMapping("/index")
public String index(#RequestParam(value="e", required=false) List<Integer> errors, Map<String, Object> model) {
model.put("indexForm", new IndexForm());
if (errors != null)
model.put("errors", errors);
return "index";
}
I don't see any differences from examples I have seen, so I would be happy if someone pointed at my mistake.

some newbies do a mistake by making the field as required in order to validate the field is filled or not
e.g in normal html -
input type="text" name="name" required
in spring - form:input path="firstName" required .... here required will not work and so same type of error "equal symbol expected".

Oh. It seems, including jsp tags one in another doesn't work. So, I changed
action="<c:url value="index_search" />"
for
action="index_search"
and received my page without errors.

Related

Data not showing in my another Html table after submitting. I am using spring mvc with thymeleaf, what might be the reason

I added thymeleaf object and variable expressions in html and written #Getmapping and #postmapping methods and separate pojo class. after submitting the data using form.html file it going to another grades.html file but data is not showing what might be the issue.
controller program
#Controller
public class piperscontroller {
List<piedpiper> addGrades= new ArrayList<>();
#GetMapping("/")
public String submitForm(Model model){
model.addAttribute("grades",new piedpiper());
return "form";
}
#PostMapping("/handleSubmit")
public String grademeth(piedpiper grades){
addGrades.add(grades);
return "redirect:/grades";
}
#GetMapping("/grades")
public String finalButton(Model model){
model.addAttribute("grades",addGrades);
return "grades";
}
}
<!DOCTYPE HTML>
<html>
<head>
<title>Form</title>
<link th:href="#{/css/form.css} " rel="stylesheet">
</head>
<body>
<div class="topnav">
<a>Form</a>
<a>Grades</a>
</div>
<div class="container">
<h2> Grade Portal </h2>
<form method="post" th:object="${grades}" th:action="#{/handleSubmit}">
<input type="text" placeholder="Name" th:field="*{name}" >
<input type="text" placeholder="Score" th:field="*{score}">
<input type="text" placeholder="Subject" th:field="*{subject}">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>

View and controller data exchange

I'm using form tag to get information of title and content.
But I can't get any information from form tag.
It happens to me several time. I don't know what is the problem..
this is the controller code
#Slf4j
#Controller
#RequiredArgsConstructor
public class PostController {
private final PostRepository postRepository;
private final MemberService memberService;
#GetMapping("/board/upload")
public String moveToUploadPage(Model model) {
model.addAttribute("uploadPostForm", new UploadPostForm());
return "board/uploadForm";
}
#PostMapping("/board/upload")
public String updateBoard(#ModelAttribute UploadPostForm uploadPostForm, Authentication authentication, Model model) {
log.info("uploadPostForm.title = {}", uploadPostForm.getTitle());
Member member = findMember(authentication);
Post post = new Post(member, uploadPostForm.getTitle(), uploadPostForm.getContent());
postRepository.save(post);
return "/board/board";
}
}
this is html code
<!DOCTYPE html>
<html lang="kr" xmlns:th="http://www.thymeleaf.org">
<head th:insert="~{fragment/header :: header}">
<meta charset="utf-8">
</head>
<body>
<div th:replace="~{fragment/navbar :: nav}"></div>
<!-- Form Tag -->
<form th:action th:object="${uploadPostForm}" method="post" class="container-md">
<div class="m-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" th:field="*{title}">
</div>
<div class="m-3">
<label for="content" class="form-label">Text Area</label>
<textarea class="form-control" id="content" rows="10" th:field="*{content}"></textarea>
</div>
<div class="container" style="text-align: right">
<span class="m-3" th:text="|writer: | + ${#authentication.getName()}"></span>
<button type="submit" class="btn btn-primary">upload</button>
</div>
</form>
</body>

How to resolve request methos 'POST' not supported?

I want to design a quick registration page which is placed at the footer when I submit the field, It is showing some error "Request method POST not supported". Someone, please help me to come out of this error.
I have modified the attribute of the form commandName as modelAttribute, but still, the error exists.
This is jsp front end.
<%# tag body-content="empty" trimDirectiveWhitespaces="true"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:url value="/setSubscribe" var="subscribe"></c:url>
<form:form action="${subscribe}" method="post" modelAttribute="subscribeForm">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<label class="form-check-label radio-inline">
<input type="radio" class="form-check-input" value="gender" name="gender" value="Male"/> Male
</label>
<label class="form-check-label radio-inline">
<input type="radio" class="form-check-input" value="gender" name="gender" value="Female"/> Female
</label>
<br></br>
<div class="row">
<div class="col-sm-3">
<input class="form-control" name="firstName" placeholder="First Name"></input>
</div>
<div class="col-sm-3">
<input class="form-control" name="lastName" placeholder="Last Name"></input>
</div>
<div class="col-sm-3">
<input class="form-control" name="email" placeholder="Your Email Address"></input>
</div>
<div class="col-sm-3">
<button type="submit" class="btn btn-primary">SUBSCRIBE</button>
</div>
</div>
</form:form>
Here is my controller.
#RequestMapping(value = "/setSubscribe", method = RequestMethod.POST)
private String doSubscribe(#ModelAttribute("subscribeForm") final SubscribeForm form)
{
final RegisterData registerData = new RegisterData();
registerData.setFirstName(form.getFirstName());
registerData.setLastName(form.getLastName());
registerData.setSex(form.getGender());
registerData.setLogin(form.getEmail());
final CerCustomerFacadeImpl customerFacadeImpl = new CerCustomerFacadeImpl();
try
{
customerFacadeImpl.newCerRegister(registerData, true);
}
catch (final Exception e)
{
e.printStackTrace();
}
return "";
}
It should have to behave according to the business which has been mentioned in the controller.
Write your controller method with #ResponseBody annotation and consumes property of #RequestMapping annotation, as following:
#RequestMapping(value = "/setSubscribe", method = RequestMethod.POST, consumes = "application/json")
#ResponseBody
private String doSubscribe(#ModelAttribute("subscribeForm") final SubscribeForm form)
{
return "";
}

How to fill List<string> field in Thymeleaf + Spring

here is my form
<!--destinationList List-->
<div class="form-group">
<div class="col-sm-3">
<label for="Destination">Destination</label>
</div>
<div class="col-sm-9">
<input type="text" class="form-control" th:field="*{destinationList[0]}" />
<input type="text" class="form-control" th:field="*{destinationList[1]}" />
<input type="text" class="form-control" th:field="*{destinationList[2]}" />
<input type="text" class="form-control" th:field="*{destinationList[3]}" />
</div>
</div>
<div class="form-group">
<div class="col-sm-9">
<button type="submit" class="btn btn-primary btn-block">Calculate</button>
</div>
</div>
</form>
And I'm going to fill following model
public class PriceSearchDTO {
private List<String> destinationList;
public List<String> getDestinationList() {
return destinationList;
}
public void setDestinationList(List<String> destinationList) {
this.destinationList = destinationList;
}
}
I can do this. But I hard coded number of input fields in the list as above in the view. I need to genarate them dynamically and make the number of element in the list is airbitary.
Try this:
<div class="col-sm-9">
<input type="text" class="form-control" th:each="destination : ${destinationList}" th:field="*{destination}" />
</div>
You are using Iteration in Thymeleaf. In fact, there is a quite complete set of objects that are considered iterable by a th:each attribute.
Like this :
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../../css/gtvg.css" th:href="#{/css/gtvg.css}" />
</head>
<body>
<h1>Product list</h1>
<table>
<tr>
<th>NAME</th>
<th>PRICE</th>
<th>IN STOCK</th>
</tr>
<tr th:each="destination: ${destinations}">
<td th:text="${destination}">Onions</td>
</tr>
</table>
<p>
Return to home
</p>
</body>
</html>
And Controller in Spring framework.
#RequestMapping(value = "/")
public ModelAndView showView(ModelAndView mav) {
List<String> destinations = new ArrayList<String>();
destinations.add("elementA");
destinations.add("elementB");
destinations.add("elementC");
mav.addObject("destinations", destinations);
mav.setViewName("/viewName");
return mav;
}

Spring - JSP form:input on jsp page error: jasperexception

When I put the code inside the form:form tag on view cadastro.jsp:
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> name Project: </ label>
<div class="col-sm-10">
<input for="nome" class="form-control" />
<form:errors path="nome" />
</div>
</div>
Its works
but when i put
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> Nome do Projeto: </ label>
<div class="col-sm-10">
<form:input cssClass="form-control" path="nome" />
<form:errors path="nome" />
</div>
</div>
not work because the form: input tag
Error:
HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/view/cadastro.jsp at line 34
ProjetroController
#RequestMapping(value = "/novoProjeto", method = RequestMethod.POST)
public String adicionarProjeto(#Valid #ModelAttribute("projeto") Projeto projeto, BindingResult result) {
if(result.hasErrors()) {
return("cadastro");
}
projeto.setStatus("NOVO");
this.pc.salvar(projeto);
return "redirect:/listar";
}
I think you need to add a <form:form> tag around your form, as follows:
<form:form>
<div class="form-group">
<label for="nome" class="col-sm-2 control-label"> Nome do Projeto: </ label>
<div class="col-sm-10">
<form:input cssClass="form-control" path="nome" />
<form:errors path="nome" />
</div>
</div>
</form:form>
In Spring official documentation, it states that:
All the other tags in this library are nested tags of the form tag.
you have to import spring form taglib inorder to use elemnets of spring form like:
add this in top of your jsp
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
then, you can use like:
<form:form id="myForm" method="post" action="/someAction" modelAttribute="formBean">
<form:label path="name"/>
<form:input path="name"/>
<form:form>
and you have add modelAttribute/command object in controller like:
#RequestMapping(value="/someUrl", method=RequestMethod.GET)
public String showForm(Model model){
model.addAttribute("formBean", new FormBean());
return "someViewName";
}
and FormBean class looks like:
public class FormBean {
private String name;
public FormBean(){} //default constructor
//getter and setter for name
}

Resources