I am developing a dashboard page which will parse files and generate charts. So when I click submit button, I want that page should get reloaded or refreshed after successful post method. I have enabled security also, but disabled csrf().
Here is my controller,
#RequestMapping(value = "/analyze.go", method = RequestMethod.POST)
#ResponseBody
public String analyze(#RequestParam("logFile") MultipartFile file) {
//my code
return "{}";
//return "redirect:dashboard.html";
}
Here is my thymeleaf template named as dashboard.html,
<form role="form" enctype="multipart/form-data" th:action="#{/analyze.go}"
method="post">
<div class="form-group">
<label for="logFile">File input</label> <input type="file"
name="logFile" id="logFile"/>
<p class="help-block">Select only *.log or any text type of
files</p>
<input type="submit" class="btn btn-primary" value="Submit"/>
</form>
Currently it is redirecting to http://localhost:9090/analyze.go page. Any idea how to overcome this issue. I tried with returning "redirect:redirect_page" but it is not working.
Related
My Java/Springboot/Thymeleaf app has a form called direct bind. On this form there is a modal that pops up when you click a button, in this modal you can attach a file. The submit button to the modal, does not seem to be registering or sending to the #Requestmapping. Any idea why and how to fix this?
I've tried setting the controller up like this instead #RequestMapping(value="/attachDoc", params="document"). I've also tried adding formnovalidate="formnovalidate" on the submit input. As well as moving it to the same controller file as the original form. Not sure what else to try.
HTML:
<div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="#{/attachDoc}" method="post" th:object="${newDocument}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="submit" name="attachNewDoc" value="Submit" class="btn btn-docModal"/></div>
</form>
</div>
</div>
Controller:
#RequestMapping(value="/attachDoc")
public String attachDoc(Model model, #ModelAttribute(value="newDocument") Document newDocument, #RequestParam("document") MultipartFile file){
Document doc=documentRepository.save(newDocument);
doc.setStorage(storageService.store(file));
documentRepository.save(doc);
return "directBind";
}
Define method=RequestMethod.POST in your mapping.
By default if you are not specifying any method in request mapping then it will be GET.
You are using post method in your thymeleaf but the mapping in the controller is for GET(By default)
So you have to change requestmapping like below.
#RequestMapping(value="/attachDoc",method=RequestMethod.POST) by specifying POST method.
I'm using thymeleaf for my web application. There is a problem about save and update functions. When I want to save a campaign from ui, the campaign object fields come null to rest controller class. This problem just occure in weblogic server(12.1.3). When I try it in tomcat server, do not occure any error.
my edit and create page as follow. There are a few fields for campaign, but I wrote some of them in here. Bytheway, I am sure that all fields ready in the html page. Some of them hidden, some of them visible.
<div class="row">
<form name="Form" class="col s8 offset-s2" id="upload-file-form"
enctype="multipart/form-data" th:object="${campaign}"
th:action="#{/admin/getCampaign}" onsubmit="return validateForm()"
method="post">
<div class="row">
<input type="hidden" th:field="*{id}"/>
<input type="hidden" th:field="*{version}"/>
</div>
<div class="row">
<div class="input-field">
<input id="brandname" type="text" class="validate" th:field="*{brandname}">
<label for="brandname">Brand Name</label>
</div>
</div>
</form>
</div>
#RequestMapping(value = "admin/getCampaign", method = RequestMethod.POST)
public String uploadingPost(#RequestParam("uploadingFiles") MultipartFile[] uploadingFiles,
#RequestParam("uploadingFiles1") MultipartFile[] uploadingFiles1,
#RequestParam("uploadingFiles2") MultipartFile[] uploadingFiles2,
#RequestParam("uploadingFiles3") MultipartFile[] uploadingFiles3,
Campaign campaign) throws IOException {
/** this is my controller method for save or update.
*/
}
in weblogic server campaign parameter fields come null (as a new object), but in tomcat server, everything is normal.
UPDATE:
I changed my ui fields to value like this post. But the problem continue.
<input type="hidden" th:value="*{id}"/>
Your form enctype is "multipart/form-data".
So you must add spring.http.encoding.enabled=false to application.properties.
It should be <input type="hidden" th:field="*{id}"/>not th:value
I'm developing a webApp in java, maven, JPA and thymeleaf and I have this problem:
I have this method in my controller:
#RequestMapping(value = "/admin/searchResults", method = RequestMethod.GET)
public String listResults(#ModelAttribute Step step,Model model,#PathVariable("dateStart") Date dateStart,#PathVariable("dateEnd") Date dateEnd){
model.addAttribute("dateStart", dateStart);
model.addAttribute("dateStart", dateEnd);
return "thisAreTheDates";
}
And this is a fragment of the view that in theory has to send the 2 date parameters to my fancy controller:
<div class="container">
<form class="form-inline" data-th-action="#{/admin/searchResults}" method="get" >
<div class="input-group">
<div class="form-group">
<label>From:</label>
<input type="date" id="dateStart" class="form-control"/>
<label>To:</label>
<input type="date" id = "dateEnd" class="form-control"/>
</div>
</div>
<br/>
<button type="submit" class="button">Generate Report</button>
</form>
</div>
Everything goes so good, until I go to that view, fill the fields with 2 dates and when I press the button of generate report, this error appears:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun May 28 10:54:07 BOT 2017
There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'dateStart' for method parameter of type Date
In your HTML, you need to add name="dateStart" and name="dateEnd" to each of your dateStart and dateEnd input fields, respectively. This will allow your form connect to your model attribute.
Additionally, you will want to add a method to your controller annotated with #PostMapping to actually handle what you want to be done with your form input.
Finally, you will not want to use get on your HTML form but more likely, post.
So:
#GetMapping("/admin/searchResults") //use the shorthand
public String listResults(#ModelAttribute("step") Step step,
#PathVariable("dateStart") Date dateStart,
#PathVariable("dateEnd") Date dateEnd,
Model model) {
Step step = new Step(); //or however you are creating this
model.addAttribute("step", step);
model.addAttribute("dateStart", dateStart);
model.addAttribute("dateStart", dateEnd);
return "thisAreTheDates";
}
Your Post Mapping:
#PostMapping("/admin/searchResults") //use the shorthand
public String postResults(#ModelAttribute("step") Step step,
Errors result) {
//validate your step bean
someValidator.validate(step, result);
//persist it, do whatever
someService.save(step);
return "confirmationPage";
}
Updated HTML:
<div class="container">
<form class="form-inline" th:object="${step}" th:action="#{/admin/searchResults}" method="post" >
<div class="input-group">
<div class="form-group">
<label>From:</label>
<input type="date" name="dateStart" id="dateStart" class="form-control"/>
<label>To:</label>
<input type="date" name="dateEnd" id="dateEnd" class="form-control"/>
</div>
</div>
<br/>
<button type="submit" class="button">Generate Report</button>
</form>
</div>
Resolution
I've now managed to get this working.
It was my own oversight that was causing the null result, and thanks to #WillKeeling for pointing out that I was mapping the form to the wrong controller method.
Rather than creating a new Role object for the form to submit, I was (for an unknown reason) asking it to submit a User object.
My working JSP & Controller below:
<form:form method="POST" action="/users/${user.username}/addRole" modelAttribute="newRole">
<div class="input-group">
<span class="input-group-addon">Roles</span>
<form:select class="form-control" path="rolename" multiple="false">
<form:options items="${roles}" title="rolename" itemValue="rolename" itemLabel="rolename" />
</form:select>
<span class="input-group-addon"><input type="submit" value="Add Role" class="btn btn-xs btn-warning"/></span>
</div>
</form:form>
Controller Mapping
#RequestMapping(value = "/{username}/addRole", method = RequestMethod.POST)
public String addUserRoles(#ModelAttribute("newRole") Role newRole, #PathVariable("username") String username) {
System.out.println(newRole.getRolename());
return "redirect:/users/" + username;
}
Original Question
I may be missing something obvious here but I was hoping someone may be able to help.
I'm building an access management system for the team I work in. I've got a form on a jsp (code below) which should submit a new "role" object that can be handled into the repository in the back end.
At the moment however, when I click submit the logging reports that the value is "null". Can somebody please shed some light on why this is?
Form in JSP
<form:form method="POST" action="/users/${user.username}/addRole" modelAttribute="user">
<div class="input-group">
<span class="input-group-addon">Roles</span>
<form:select class="form-control" path="roles" multiple="false">
<form:options items="${roles}" title="roles" itemValue="rolename" itemLabel="rolename" />
</form:select>
<span class="input-group-addon"><input type="submit" value="Add Role" class="btn btn-xs btn-warning"/></span>
</div>
</form:form>
Controller
Original path is /users with the following controller.
#RequestMapping(value = "/{username}/addRole", method = RequestMethod.POST)
public String addUserRoles(#ModelAttribute("user") User user) {
System.out.println(user.getRoles());
return "redirect:/users/" + user.getUsername();
}
Result in Console
Role [rolename=null]
I am trying to find a way to do some Javascript Form Validation with Spring WebFlow.
I have the following code in my JSP
<INPUT tabIndex="46" value="Submit" type="submit" name="_eventId_submit" onClick="return dontSubmit();">
that runs a JavaScript function and returns false is the form does not pass validation but my page is still getting submitted to the server. I don't want it submitted if I found a validation error
I also tried:
<INPUT tabIndex="46" value="Submit" type="submit" name="_eventId_submit" onsubmit="return dontSubmit();"
and again if find a error the form still gets submitted. please I tried:
<form:form modelAttribute="visit" action="${flowExecutionUrl}" onsubmit="dontSubmit()">
and again the form gets submitted with and without errors..... please help below you will find the funcation
function dontSubmit()
{
alert("DONT SUBMITTED TO SERVER");
return false;
}
Generally a form validation is done using something like the following:
<form action="server.action" method="POST" onSubmit="return validate(this);">
<input .../>
<input type="submit" name="Submit the form"/>
</form>
where this in the validate function is a reference to the form
Place the onsubmit on the FORM tag:
<FORM action="..." onsubmit="return checkrequired();" method="POST">
<INPUT tabIndex="46" value="Submit" type="submit" name="_eventId_submit" >
See JavaScript Form Validation.