Spring basic form error 405 - java

Hi i got this error try to learn some of Spring Java framrework.
I got an 405 - Request method 'POST' not supported and i need some help to see what is my error on this
my controller
#Controller
#RequestMapping("overcomandant/addSitio.asp")
public class addSitioController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView addSitioForm() {
ModelAndView asf = new ModelAndView();
asf.setViewName("admin/addNewSite");
asf.addObject("sitio", new Sitio());
return asf;
}
#RequestMapping(value="admin/addNewSite", method = RequestMethod.POST)
public String addSitioSubmit(Sitio st, ModelMap model) {
model.addAttribute("url", st.getUrl());
model.addAttribute("nombre", st.getNombre());
model.addAttribute("estado", st.getEstado());
return "admin/exito";
}
#ModelAttribute("estadoLista")
public Map<String,String> ListadoEstados() {
Map<String, String> estado = new LinkedHashMap<>();
estado.put("1","Activo");
estado.put("2","Inactivo");
estado.put("3","Testing");
return estado;
}
}
and this is my form addNewSite.jsp
<form:form method="POST" commandName="sitio">
<div class="form-group">
<form:label path="id">ID</form:label>
<form:input path="id" cssClass="form-control"/>
</div>
<div class="form-group">
<form:label path="url">URL</form:label>
<form:input path="url" cssClass="form-control"/>
</div>
<div class="form-group">
<form:label path="nombre">Nombre</form:label>
<form:input path="nombre" cssClass="form-control"/>
</div>
<div class="form-group">
<form:label path="estado">Estado</form:label>
<form:select path="estado" cssClass="form-control">
<form:option value="0">Seleccione</form:option>
<form:options items="${estadoLista}" />
</form:select>
</div>
<input type="submit" value="Enviar" class="btn btn-primary" />
</form:form>
and the exito.js
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<p><c:out value="${url}"></c:out></p>
</body>
</html>
I try to understand what is worng.
The controller creates an object site adding the info form the form an then the otrer .jsp renders the new object created...

You have to specify your form action to correspond the method in your controller : admin/addNewSite.
The 405 error tells you that the form action is unknown.

Related

Thymeleaf th:text does not work in some case

I started to work with thymeleaf in a basic project. When I want to display a message in the sign up page the th:text does not work. I used already th:text in other .html files and it works there but in the signUp.html doesn't.
Here is my controller where I set the message:
package com.teszt.thymeleaftest.controller;
#Controller
#RequestMapping("/login")
#AllArgsConstructor
public class MemberController {
MembersService membersService;
#GetMapping("/login")
public String showLoginPage(){
return "/login/login";
}
#GetMapping("/signUp")
public String showSignUpPage(Model theModel){
Members theMember = new Members();
theModel.addAttribute("member", theMember);
return "/login/signUp";
}
#PostMapping("/save")
public String saveMember(#ModelAttribute("member") Members theMember, ModelMap modelMap){
Members tempMember = membersService.findByEmail(theMember.getEmail());
if(tempMember != null){
modelMap.addAttribute("error", "Email is already exist!");
return "redirect:/login/signUp";
}else{
membersService.save(theMember);
//prevent duplication
return "redirect:/login/login";
}
}
}
Here is my signUp.html
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>Signup</title>
</head>
<body>
<div class="container">
<h3>Signup</h3>
<hr>
<p class="h4 mb-4">Signup</p>
<form action="#" th:action="#{/login/save}" th:object="${member}" method="POST">
<input type="text" th:field="*{firstName}" class="form-control mb-4 col-4" placeholder="First name">
<input type="text" th:field="*{lastName}" class="form-control mb-4 col-4" placeholder="Last name">
<input type="text" th:field="*{email}" class="form-control mb-4 col-4" placeholder="Email">
<input type="password" th:field="*{password}" class="form-control mb-4 col-4" placeholder="Password">
<button type="submit" class="btn btn-info col-2">Save</button>
<p th:text="${error}" />
</form>
<br>
Do you have an account? <a th:href="#{/login/login}">Click here</a>
</div>
</body>
</html>
As I mentioned above this is the only html where the th:text does not works, everywhere else is good.
I hope somebody can help me!
When you use a redirect:, you lose all your model attributes (because it's loading a new page). You need to use a FlashAttribute instead. Like this:
#PostMapping("/save")
public String saveMember(#ModelAttribute("member") Members theMember, ModelMap modelMap, RedirectAttributes redirAttrs){
Members tempMember = membersService.findByEmail(theMember.getEmail());
if(tempMember != null){
redirAttrs.addFlashAttribute("error", "Email is already exist!");
return "redirect:/login/signUp";
}else{
membersService.save(theMember);
//prevent duplication
return "redirect:/login/login";
}
}

GET parameter to the list in Spring-boot

I have to submit name from the form and receive the value in GET method.
Received value in URL should be shown up on top of form as a list.
I want to show all input from form submit as unordered list.
But my code only shows the latest input.
Friend class has
private String friend and getter setters.
#Controller
public class FriendController {
#GetMapping(value = "/index")
public String friendForm(#RequestParam(value = "friend", required = false) Friend friend, Model model) {
List<Friend> fList = new ArrayList<Friend>();
fList.add(friend);
model.addAttribute("friends", fList);
model.addAttribute("friend", new Friend());
return "result";
}
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Friends List</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'My Friends'"></h1>
<ul>
<li th:each="friend: ${friends}" th:text="${friend}"></li>
</ul>
<h1 th:text="'Add a new friend to List'"></h1>
<form action="#" th:action="#{/index}" th:object="${friend}"
method="get">
<table>
<tr>
<td>Name: <input type="text" th:field="*{friend}" /></td>
</tr>
</table>
<p>
<input type="submit" value="Submit" />
</p>
</form>
</body>
</html>

How to return value in #PostMapping

I'm pretty new in Spring boot and have some problems.
I have automobile and file, when I add automobile data and save it in db, want to upload the file which is connected to automobile by relationship OneToOne but when save automobile in #PostMapping("/saveAutomobile") I could't take this automobileId and to transfer it to #PostMapping("/uploadFile") to do the connection. Do you have any suggestions?
adding automobile:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<title>New Brand</title>
<link th:href="#{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
<div class="container add shadow">
<h2>New Automobile</h2>
<form th:action="#{/automobile/saveAutomobile}" method="post"
th:object="${automobileForm}">
<div class="row">
<div class="column">
<div class="form-group shadow">
<label class="form-control-label" for="inputBrand"> Brand</label>
<input type="text"
class="form-control form-control-danger box-shadow"
id="inputBrand" th:field="*{brand}" name="brand"
required="required" />
</div>
<div class="form-group shadow">
<label class="form-control-label" for="inputModel"> Model</label>
<input type="text"
class="form-control form-control-danger box-shadow"
id="inputModel" th:field="*{model}" name="model"/>
</div>
</div>
<div style="text-align: center;">
<a th:href="#{/file/uploadFile} + ${automobile?.getId()}"> <input type="submit"
class="btn btn-default box-shadow shadow" />
</a>
</div>
</form>
</div>
<script th:src="#{resources/js/jquery-1.11.1.min.js}"></script>
<script th:src="#{resources/js/bootstrap.min.js}"></script>
</body>
</html>
#PostMapping("/saveAutomobile")
public String saveAutomobile(#Valid final AutomobileForm automobileForm, final BindingResult result) {
if (result.hasErrors()) {
LOG.error("SaveAutomobile Error: " + result);
return "automobile/add";
}
final Automobile automobile = automobileConverter.ConvertToEntity(automobileForm);
if (LOG.isDebugEnabled()) {
LOG.info("Saving client " + automobile);
}
automobileService.save(automobile);
automobileForm.setId(automobile.getId());
return "/files/uploadFile";
}
uploading file:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<div th:replace="fragments/links"></div>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
<title>Upload automobile documentation</title>
<link th:href="#{resources/css/bootstrap.min.css}" rel="stylesheet"></link>
</head>
<body>
<div class="container upload shadow">
<h2>Upload automobile documentation</h2>
<form th:action="#{/uploadFile}" method="post"
enctype="multipart/form-data" id="singleUploadForm"
name="singleUploadForm" >
<div class="form-group shadow">
<label class="form-control-label" for="uploadfile">Upload
File:</label> <input id="singleFileUploadInput" type="file" name="file"
class="file-input form-control form-control-danger box-shadow" />
</div>
<button type="submit" class="btn btn-default box-shadow shadow"
id="btnSubmit">Upload</button>
</form>
<div class="upload-response">
<div id="singleFileUploadError"></div>
<div id="singleFileUploadSuccess"></div>
</div>
</div>
<script src="/js/main.js"></script>
</body>
</html>
#Autowired
private FileStorageService DBFileStorageService;
#PostMapping("/uploadFile")
public FileForm uploadFile(#RequestParam("file") MultipartFile file) {
File dbFile = DBFileStorageService.storeFile(file);
return new FileForm(dbFile.getFileName(),
file.getContentType(), file.getSize());
}
You can use models to store data and use it in your HTML page
for example:-
Controller.java
#GetMapping("/goToViewPage")
public ModelAndView passParametersWithModelAndView() {
ModelAndView modelAndView = new ModelAndView("viewPage");
modelAndView.addObject("message", "Hello from the controller");
return modelAndView;
}
Test.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
</head>
<body>
<div>Web Application. Passed parameter : th:text="${message}"</div>
</body>
</html>
#PostMapping("/uploadFile")
public ResponseEntity<FileForm> uploadFile(#RequestParam("file") MultipartFile file) {
File dbFile = DBFileStorageService.storeFile(file);
FileForm fileForm = new FileForm(dbFile.getFileName(), file.getContentType(), file.getSize());
return new ResponseEntity<>(fileForm, HttpStatus.OK);
}

how to bind HTML5 and SpringMVC 4 using Thymeleaf?

OK, I managed to get my application using Thymeleaf and now I need to reconstruct all the JSP pages into HTML5. First and foremost, I need to be able to login into the application using the username and password that worked very nicely on JSP.
My bean for assigning the HTML5 pages as the view layer is defined as:
#Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);
return viewResolver;
}
My LoginController is defined as:
#Controller
public class LoginController {
#Autowired
UserProfileService userProfileService;
#Autowired
UserService userService;
#RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
public String homePage(ModelMap model) {
String user = getPrincipal();
model.addAttribute("greeting", "Hi, Welcome to HRM");
model.addAttribute("user", user);
model.addAttribute("roles", initializeProfiles());
return "welcome";
}
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage() {
return "login";
}
#RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/login?logout";
}
private String getPrincipal(){
String userName = null;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
userName = ((UserDetails)principal).getUsername();
} else {
userName = principal.toString();
}
return userName;
}
(...)
}
The old login.jsp is transcribed bellow:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%#include file="include/include.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login page</title>
<%#include file="include/imports.jsp"%>
</head>
<body>
<div id="mainWrapper">
<div class="login-container">
<div class="login-card">
<div class="login-form">
<c:url var="loginUrl" value="/login" />
<form action="${loginUrl}" method="post" class="form-horizontal">
<c:if test="${param.error != null}">
<div class="alert alert-danger">
<p>Invalid username and password.</p>
</div>
</c:if>
<c:if test="${param.logout != null}">
<div class="alert alert-success">
<p>You have been logged out successfully. </p>
</div>
</c:if>
<div class="input-group input-sm">
<label class="input-group-addon" for="username"><i class="fa fa-user"></i></label>
<input type="text" class="form-control" id="username" name="ssoId" placeholder="Enter Username" required>
</div>
<div class="input-group input-sm">
<label class="input-group-addon" for="password"><i class="fa fa-lock"></i></label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="form-actions">
<input type="submit"
class="btn btn-block btn-primary btn-default" value="Log in">
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
The new login.html is transcribed bellow:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8"></meta>
<title>Login Page</title>
<meta name="description" content="login page" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="imports" href="include/imports.html" /> -->
<link rel="shortcut icon" href="assets/img/favicon.png"
type="image/x-icon" />
<link href="assets/css/bootstrap.min.css" rel="stylesheet" />
<link id="bootstrap-rtl-link" href="" rel="stylesheet" />
<link href="assets/css/font-awesome.min.css" rel="stylesheet" />
<link
href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300"
rel="stylesheet" type="text/css" />
<link id="beyond-link" href="assets/css/beyond.min.css" rel="stylesheet" />
<link href="assets/css/demo.min.css" rel="stylesheet" />
<link href="assets/css/animate.min.css" rel="stylesheet" />
<link id="skin-link" href="" rel="stylesheet" type="text/css" />
<script src="assets/js/skins.min.js" ></script>
<script src="assets/js/jquery-2.0.3.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/js/beyond.js"></script>
</head>
<body>
<div class="login-container animated fadeInDown">
<div class="loginbox bg-white">
<div class="loginbox-title">SIGN IN</div>
<div class="loginbox-textbox">
<input type="text" class="form-control" placeholder="Username"></input>
</div>
<div class="loginbox-textbox">
<input type="password" class="form-control" placeholder="Password"></input>
</div>
<div class="loginbox-forgot">
Forgot Password?
</div>
<div class="loginbox-submit">
<input type="button" class="btn btn-primary btn-block" value="Login"></input>
</div>
<div class="loginbox-signup">
Sign Up With Email
</div>
</div>
</div>
</body>
</html>
I really am not familiar with how Thymeleaf and HTML5 handles requests. On the JSP form, the action was explicit and the submit type button or link would access the POST method on the controller and process the request. But how do I bind the view (Thymeleaf + HTML5) to the controller (Spring) layer?
Looking at your code, you have a form with both action and method attributes specified in your JSP template, but you're lacking a form entirely in your Thymeleaf template. You should be able to re-create the same form with either th:attr, th:action or th:method attributes. With properly mapped request, your controller should handle it no problem.
For more details, you can have a look at section 5 of Thymeleaf docs:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-attribute-values

Neither BindingResult nor plain target object for bean name 'owner' available as request attribute

I am trying to insert data's into database using Spring,Hibernate and MySql. I am getting the following error while inserting data into database
"Neither BindingResult nor plain target object for bean name 'owner' available as request attribute". I tried all possible solutions available but couldn't clear it.
This is my controller
#Controller
public class DataOwnerRegController {
#Autowired(required = true)
DataService dataService;
#RequestMapping(value="/DataOwnerReg",method = RequestMethod.POST)
public String getForm(#ModelAttribute("owner") Model model )
{
model.addAttribute("owner", new Owner());
return "DataOwnerReg";
}
#RequestMapping(value = "/DataOwnerReg")
public ModelAndView registeruser(#ModelAttribute("owner") Owner owner, BindingResult result){
ModelAndView modelAndView = new ModelAndView("DataOwnerReg");
dataService.insertRow(owner);
return modelAndView;
}
}
My View
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="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>DATA OWNER REGISTRATION</title>
<link href="<c:url value="/resources/theme/css/bootstrap.css"/>" rel="stylesheet" >
<link href="<c:url value="/resources/theme/css/style.css"/>" rel="stylesheet">
<script src="<c:url value="/resources/theme/js/jquery-2.1.4.min.js"/>"> </script>
<script src="<c:url value="/resources/theme/js/bootstrap.mi.js"/>"></script>
<link href='https://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Bangers' rel='stylesheet' type='text/css'>
</head>
<body class="body-image">
<div>
<nav style="background-color:transparent;border:none" class="navbar navbar-default">
<div class="container">
<ul class="nav navbar-nav">
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/"/>">HOME</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">DATAOWNER</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">DATA CENTER</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">KEY DISTRIBUTION</a></li>
<li><a style="font-family: Raleway,sans-serif;color:white;font-size:15px;font-weight:600" href="<c:url value="/DataOwner"/>">ADMINISTRATOR</a> </li>
</ul>
</div>
</nav>
</div>
<div class="container">
<div class="border">
<form:form modelAttribute="owner" method="Post" action="/DataOwnerReg">
<h3 style="text-align:center">Data Owner Registration</h3>
<h2 style="text-align:center">${message}</h2>
<hr style="width:25%"/>
<div class="formation">
<h4>Username:</h4>
<div class="input-group">
<form:input type="text" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Enter the Username" required="" path="Username" /><div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
</div>
<h4>Password:</h4>
<div class="input-group">
<form:input type="password" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Enter the Password" required="" path="Password"/><div class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></div>
</div>
<h4>Confirm Password:</h4>
<div class="input-group">
<form:input type="password" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="Repeat Password" required="" path="CPassword" /><div class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></div>
</div>
<h4>Email Address:</h4>
<div class="input-group">
<form:input type="email" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="me#example.com" required="" path="Email" /><div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div>
</div>
<h4>Mobile Number:</h4>
<div class="input-group">
<form:input type="text" class="form-control" style="font-family:Raleway,sans-serif;height:40px" placeholder="10 Digit Mobile Number" required="" path="Email" /><div class="input-group-addon"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></div>
</div>
<div>
<input type="submit" style="width:150px;height:45px;margin-top:30px;font-family:Raleway,sans-serif" class="btn btn-success" value="Register"/>
</div>
<br/>
</div>
</form:form>
</div>
</div>
</body>
</html>
Please Help in this.
Thanks in advance
There are a couple things wrong with your controller
First you have the #ModelAttribute on a method argument of the type Model which you shouldn't do. Next the method you expect to be invoked on a GET request is going to be invoked on a POST request so basically there will be no model. Finally in your method that should handle the POST request you are returning a ModelAndView but with an empty model, so at that point there is no model anymore and no Owner object.).
I would suggest to fix your controller in the following way
#Controller
#RequestMapping("/DataOwnerReg")
public class DataOwnerRegController {
private final DataService dataService;
#Autowired
public DataOwnerRegController(DataService DataService) {
this.dataService=dataService;
}
#ModelAttribute
public Owner owner() {
return new Owner();
}
#RequestMapping(method = RequestMethod.GET)
public String get() {
return "DataOwnerReg";
}
#RequestMapping(method = RequestMethod.POST)
public String registeruser(#ModelAttribute("owner") Owner owner, BindingResult result){
dataService.insertRow(owner);
return "DataOwnerReg";
}
}

Categories