JSP set boolean value in form - java

I have a Model:
public class Header {
private Boolean SERVICE;
}
Controller:
#RequestMapping("mymodel/Edit")
public ModelAndView mymodelEdit(
#ModelAttribute("mymodel") Mymodel mymodel,
#RequestParam String id) {
Mymodel old_mymodel = mymodelService.getMymodel(id);
Map<String, Object> map = new HashMap<String, Object>();
map.put("old_mymodel", old_mymodel);
return new ModelAndView("mymodel/mymodelEditView", "map", map);
}
JSP Form
<c:set var="old_mymodel" value="${map.old_mymodel}" />
<form:form method="POST action="/mymodel/Save" modelAttribute="mymodel">
<tr>
<td>Сервис :</td>
<td>
<form:checkbox path="SERVICE" value="${old_mymodel.SERVICE}">
</form:checkbox>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</form:form>
My problem: I can't set a value from db to form value, i.e. when SERVICE value is true, checkbox is not checked.

The way you are trying to access the model does not correspond to the way you have populated it.
I propose you change your code to:
#RequestMapping("mymodel/Edit")
public ModelAndView mymodelEdit(
#ModelAttribute("mymodel") Mymodel mymodel,
#RequestParam String id) {
Mymodel old_mymodel = mymodelService.getMymodel(id);
return new ModelAndView("mymodel/mymodelEditView", "model", old_mymodel);
}
and
That is assuming that Mymodel looks something like:
public class Mymodel {
private Header old_header;
}
Also there might be some problems with the names you have used in various parts of the model. I strongly suggest that you adhere to JavaBean naming conventions

first thing you are setting the value of map to variable like below
<c:set var="old_header" value="${map.old_mymodel}" />
so you have to access the SERVICE boolean value using this variable not map.
so it should be accessed like below
<td><form:checkbox path="SERVICE" value="${old_header.SERVICE}"></form:checkbox></td>
instead of
<td><form:checkbox path="SERVICE" value="${old_mymodel.SERVICE}"></form:checkbox></td>
where you are using old_mymodel,
assuming below code returns correct model
Mymodel old_mymodel = mymodelService.getMymodel(id);

Related

#ModelAttribute is returning null values with thymeleaf

After days of research I have trouble finding a solution for my controller methods. I have two controller methods that seem to be problematic. The savings method is saving null values. I have no clue how to bind the values, that are being typed into the field, to the list /all. One for creating values with input fields and another to save the input values and update a list /all. I want to get the values that I put into the fields of the form and have a updated list /all, with the new values. Note that I am only trying to save two of eleven attributes of the entire class. The class has double values and true/false. Those are being saved into db, except for the important String values. Thanks ahead for the help!
First method:
#GetMapping("/create")
public String showCreateForm(Model model, Branch branch) {
List<Branch> branches = new ArrayList<>();
BranchCreationDto branchesForm = new BranchCreationDto(branches);
for (int i = 1; i <= 1; i++) {
// the input field
branchesForm.addBranch(new Branch());
}
model.addAttribute("form", branchesForm);
return "branches/create";
}
This method is has one input field, where values of Branch can be set.
Second method:
#PostMapping("/saving")
public String saveBranches(#ModelAttribute BranchCreationDto form, Model model, Branch branch) {
// saves null but needs to be saving the values that are being typed into the field
this.branchRepository.saveAll(form.getBranches());
model.addAttribute("branches", branchRepository.findAll());
return "redirect:/all";
}
This method appears to have the problem at
this.branchRepository.saveAll(form.getBranches());
It is returning null values. I have already tried putting branch.getName(), branch.getType() into the parameter. This does not work.
With the method /all the programm is returning the list.
#GetMapping("/all")
public String showAll(Model model) {
model.addAttribute("branches", branchRepository.findAll());
return "branches/all";
}
This is my wrapper class
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public class BranchCreationDto {
#Autowired
private List<Branch> branches;
public BranchCreationDto(List<Branch> branches) {
this.branches = branches;
}
public BranchCreationDto() {
}
public void addBranch(Branch branch) {
this.branches.add(branch);
}
public List<Branch> getBranches() {
return branches;
}
public void setBranches(List<Branch> branches) {
this.branches = branches;
}
}
And this is the form
<body>
<!-- Save -->
<form action="#" th:action="#{saving}" th:object="${form}"
method="post">
<fieldset>
<input type="submit" id="submitButton" th:value="Save"> <input
type="reset" id="resetButton" name="reset" th:value="Reset" />
<table>
<thead>
<tr>
<th>Branch</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr th:each="branch, itemStat : *{branches}">
<td><input th:field="*{branches[__${itemStat.index}__].branch}" /></td>
<td><input th:field="*{branches[__${itemStat.index}__].type}" /></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
You are doing wrong ,value do not pass from controller to thyme leaf by this way.I ll give you one example.
Example:
Controller:
Model and View model =new Model and View();
model .put ("branches", branch Repository.find All());
return model;
Thyme leaf:
<input id="branches" type="hidden" t h:value="${branches}" />
Now on your value is pass to id branches.
Just add the field as a hidden input and the thymeleaf will send your changes:
<input type="hidden" th:field="*{branches}"/>

How to submit a list of checkmark values into a form in Thymeleaf?

I am trying to create a table that displays a list of all logs that have been added. In addition to displaying the info I wanted to have another column of checkboxes that when clicked would allow me to delete them with the corresponding delete button.
The issue that I am having is that I am unable to put the values from my checkboxes into the array of Longs. I also want to keep the functionality of my table as it displays correctly.
For my table I have the following code:
<form method="post" th:action="#{/projects/log/delete/}" th:object="${deleteForm}">
<div th:each="log : ${allLogs}" class="row">
<tbody>
<tr class="active">
<td>
<input type="checkbox" th:field="*{logIds}" th:value="${log.id}" />
</td>
<td th:text="${log.teamUsed}"></td>
<td th:text="${log.opponentStarters}"></td>
<td th:text="${log.opponentOthers}"></td>
<td th:text="${log.myStarters}"></td>
<td th:text="${log.myOthers}"></td>
<td th:text="${log.result}"></td>
</tr>
</tbody>
</div>
<button type="submit" id="deleteButton" class="hidden"></button>
</form>
The form that I am trying to place the checkbox values into is: (log.id is a long)
public class LogDeleteForm {
private List<Long> logIds = new ArrayList<>();
public List<Long> getLogIds() {
return logIds;
}
public void setLogIds(List<Long> logIds) {
this.logIds = logIds;
}
}
In my controller I have the following setup for my view:
#RequestMapping(value = "pokemon_log", method = RequestMethod.GET)
public String view(Model model) {
model.addAttribute("addForm", new logForm());
model.addAttribute("deleteForm", new logDeleteForm());
model.addAttribute("allLogs", logService.getAllLogs());
return "log";
}
I am able to implement the deletion fine I am just unable to get the Ids that I would like to delete. How can I get the checkbox values placed into the list of longs?
Turns out that my issue was in my deleteLogs method:
#RequestMapping(value = "/log/delete", method = RequestMethod.POST, params = "delete")
public String deleteLogs(#ModelAttribute("deleteForm") logDeleteForm deleteForm) {
List<Long> formIds = deleteForm.getLogIds();
if (formIds == null || formIds.size() == 0) {
return "redirect:/projects/log";
}
for (Long id : formIds) {
logService.deleteLog(id);
}
return "redirect:/projects/log";
}
My redirects were both "redirect:/log" instead of "redirect:/projects/log"
Also my button was missing name="delete" because it was unable to qualify as a submit with a delete param.

How do I capture multiple input text fields data into a list and iterate it over using JSTL?

I am trying to do below. In my JSP, I created a table with heading such as "Oct 2013", "Nov 2013", ....for next 12 months. i.e. till "Sept 2014".
These months and years should current, for e.g. if the month is Nov 2013 then the system should detect is Nov 2013 and it should add 12 months till next year Oct 2014.
There are 12 input fields (one for each month), and they are in a single row.
The user can add a new row dynamically in this table and enter can enter quantity values into those text fields for any of the input fields for the months.
<table border="1" class="atable" align="center" width="85%">
<tr>
<th>LRN Required</th>
<th>Oct 2013</th>
<th>Nov 2013</th>
<th>Dec 2013</th>
<th>Jan 2014</th>
<th>Feb 2014</th>
<th>Mar 2014</th>
<th>Apr 2014</th>
<th>May 2014</th>
<th>June 2014</th>
<th>Jul 2014</th>
<th>Aug 2014</th>
<th>Sept 2014</th>
</tr>
<tr>
<td></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
<td><input type="text" size="4"/></td>
</tr>
</table>
I am using Spring MVC and would like to use JSTL for this functionality.
Model class:
public class Forecast {
private int id;
private int quantity;
private String lastUpdatedBy;
private String rateCenter;
.....
getters and setters here...
}
My controller class looks like below...
#Controller
#SessionAttributes("forecastModel")
public class ForecastController extends PasBaseController {
#Autowired
HttpServletRequest request;
protected static Logger log = Logger.getLogger(ForecastController.class);
private static final String FRCST_MODEL = "forecastModel";
#RequestMapping(value="preforecast.do")
public String setUpPreforecast(final Model model, HttpServletRequest req)
{
User user = WebUtil.getSignInUser(req);
ForecastModel forecastModel = new ForecastModel();
log.debug("User Info.");
log.debug(user.getUserId());
log.debug(user.getFullName());
log.debug(user.getPhone());
Long id = (long) 15260;
List<Long> userNpaList = null;
List<String> userOcnList = null;
try{
if(user != null)
{
userNpaList = PasServicesUtils.getAllNpaAssocForUserId(id);
userOcnList = PasServicesUtils.geAllOcnAssocForUserId(id);
model.addAttribute("userNpaList", userNpaList);
model.addAttribute("userOcnList", userOcnList);
forecastModel.setUserNpaList(userNpaList);
forecastModel.setUserOcnList(userOcnList);
model.addAttribute("forecastModel", forecastModel);
}
}catch(Exception e){
log.error("List is NULL");
}
log.debug("Exiting setUpPreforecast() method.");
return PasConstants.PREFORECAST;
}
#RequestMapping(value = {PasConstants.FORECAST})
public String continueFromPreforecast(#ModelAttribute("forecastModel") ForecastModel forecastModel, Errors errors, HttpServletRequest request, final ModelMap modelMap) throws Exception
{
User user = WebUtil.getSignInUser(request);
modelMap.addAttribute("ocn", forecastModel.getOcn());
modelMap.addAttribute("phone", user.getPhone());
modelMap.addAttribute("fax", user.getFax());
modelMap.addAttribute("email", user.getEmail());
modelMap.addAttribute("forecastRptDt", PasDate.displayDayMonthYearFormat2(new PasDate()));
modelMap.addAttribute("npa", forecastModel.getNpa());
List<String> rateCntrAbbrList;
rateCntrAbbrList = PasServicesUtils.getAllRtCntrsAssocForNpa(forecastModel.getNpa());
if(rateCntrAbbrList != null && rateCntrAbbrList.size() > 0)
{
modelMap.addAttribute("rateCntrAbbrList", rateCntrAbbrList);
}
//modelMap.addAttribute("rateCtr", forecastModel.getRtCntrId().
//validateForecastData(forecastModel, errors, user);
if (errors.hasErrors())
{
return PasConstants.PREFORECAST;
}
return PasConstants.FORECAST;
}
#RequestMapping(value = {PasConstants.FORECAST_SUCCESS}, method = RequestMethod.POST)
public String submitForecast(#ModelAttribute("forecastModel") ForecastModel forecastModel, Errors errors, HttpServletRequest request, final ModelMap modelMap) throws Exception
{
/* I am trying to access the months related data i.e. quantiy that user entered through the text fields in the above table. */
ForecastServiceClient.createForecast(forecastModel);
return PasConstants.FORECAST_SUCCESS;
}
My question is, how to capture user entered data into a list and pass it to the the controller class?
Do i need to create a separate class for 12 months and a year?
Do I need to access them using a public List<String> getMonthsAndYear() {..} inside my 'Forecast' class since these months and a current year will be a part of this class only.
How do i iterate through list of months inside the JSP using JSTL? I do not want to use scriplet here.
Please, help me how to approach this problem so that the data entered by the user into input fields can be posted to the controller method for further processing.
Thanks,
Model
public class Forecast {
...
private List<String> sections;
/* getters/setters */
}
controller
#RequestMapping(method = RequestMethod.POST, value = "/test.do")
public String someController(#ModelAttribute Forecast forecast ) {
/* Do whatever with your */
}
John, thanks for your reply. I did some research and found that .... can bind a dynamic list. I modified my controller class, I updated my model class and added a list getter and setter methods as you mentioned above, also, I am now using AutoPopulatingList of spring framework to modify the list and pass it to the JSP via modelMap.addAttribute("forecastMonthsBeanList", forecastModel.getForecastMonthsBeanList()), from Controller method. My issue is,however, on the jsp side, I have this jQuery function which is supposed to create a new row every time it is clicked. i.e. the row contains 12 input text boxes/fields and upon single click it is supposed to create 12 new text boxes in a new row, but, it does not work. I am not sure how to make this jQuery function work with spring:bind when "add row" button is clicked. My 1st row is having 12 input text boxes with default values assigned as '0' from a bean declared as a getter and setter in the model class. Below is my jQuery function and JSP code.
<script type="text/javascript">
$("document").ready(function(){
$(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
$(".delRow").btnDelRow();
});
</script>
Oct 2013Nov 2013Dec 2013Jan 2014Feb 2014Mar 2014Apr 2014
May 2014June 2014Jul 2014Aug 2014Sept 2014
-----

FreeMarker Form for nested Object

I am trying to write freemarker template but could not able to parse with my object class.
My POJO is
public class Metrix {
#Id
String _id;
String loginId;
Date date;
List<MatrixDetail> headers;
//All getters and setters
}
public class MatrixDetail {
String header;
int time;
String detail;
//All getters and setters
}
//Controller after saving form
#RequestMapping(value = "/matrix/save", method = RequestMethod.POST)
public View saveMatrix(#ModelAttribute Metrix matrix, ModelMap model) {
System.out.println("Reachecd in matrix save" );
return new RedirectView("/TrackerApplication/header.html");
}
FTL template form part
<form name="matrix" action="matrix/save.html" method="post">
<table class="datatable" align:"center">
<tr>
<th>Login Id:</th> <th> <input type="text" name="loginId" value= ${matrixList.loginId} required /> </th>
</tr>
<tr> <td></td><td></td><td></td></tr>
<tr>
<th>Header</th> <th>Time</th> <th>Details</th>
</tr>
**// I am not getting how this nested object which is of type List<MatrixDetail>
// will get parse in my form.**
<#list matrixList.headers as header>
<spring:bind path = "MatrixDetail">
<tr>
<td> <input name = "header" value = ${header.header} /> </td>
<td> <input name = "time" value = ${header.time} /> </td>
<td> <input name = "detail" value = ${header.detail} /></td></tr>
</#list>
</table>
<input type="submit" value="Save" />
</form>
How can we write freemarker template for form processing of such kind of nested object?
I am getting issues in form submission.
I would strongly advise against this.
Forms might be displayable in email in some cases, but they may not always work in the email client, not to mention those that only ever read emails in text-only form won't be able to use them whatsoever.
If you need users to enter a form, link to a page on your site and have the form there instead.

Spring MVC - Child entity lost after submit

I'm going to try to explain my problem as completely and shortly as I can...
A web application, made on Spring MVC 2.5 + Hibernate + Java 6 (not using annotation!).
I've got a controller extending SimpleFormController and a jsp page that is its formView and successView.
This controller should help me to insert into db an entity PracticeT that has connected (many to one) a lookup entity PracticeConfT (think about it as a "typology"). I need to choose that "typology" through a drop-down menu. In my webapp I need to be able to save data inserted and when I want, to submit the request for approval.
The page has some text fields and that drop-down menu. The bean called as default "command" is NewPracticeBean that has within a reference to an object PracticeT.
THE PROBLEM IS: I fill the form, I select a typology from the drop-down menu, I submit form and save data on DB but when I come back to the view, every property is there but the drop-down menu it is not: it has all the options allowed but no one selected. Some checks revealed that the entity PracticeConfT is null (but it has been recorded on db correctly and debugging it is still there in the model until the very end of the method onSubmit!!!).
I hope someone can help me. Thank you in advance!
Bye,
Dolfiz
Here some useful code:
(I don't think that hibernate config can be the problem, but if you need it, I can post it too)
newPractice.jsp
<form:form id="newPracticeForm" commandName="command">
<input type="hidden" name="action"/>
<spring:nestedPath path="practiceT">
<table class="table-data-form">
<tr>
<td class="left"><spring:message code="" text="Practice type" /></td>
<td>
<form:select path="practiceConfT" multiple="false">
<form:option value="" label="- seleziona -"/>
<form:options items="${practiceTypeList}" itemValue="idPracticeConf" itemLabel="practiceName"/>
</form:select>
</td>
</tr>
<tr>
<td class="left">
<spring:message code="" text="Opzione divisa" />
<br/><form:errors cssClass="errors" path="opzioneDivisa" />
</td>
<td><form:input path="opzioneDivisa" /></td>
</tr>
<tr>
<td colspan="1">
<input type="submit" name="submit" id="submit" value="Save" class="buttonEMS" style="width:100px;" />
</td>
</tr>
</table>
</spring:nestedPath>
</form:form>
NewPracticeBean.java
public class NewPracticeBean implements Serializable{
private PracticeT practiceT;
private String action;
private boolean typeSelected;
public NewPracticeBean(){
super();
this.practiceT = new PracticeT();
}
// getters & setters...
}
PracticeT.java
public class PracticeT implements java.io.Serializable {
private long idPractice;
private PracticeConfT practiceConfT;
private String opzioneDivisa;
// getters & setters...
}
PracticeConfT.java
public class PracticeConfT implements java.io.Serializable {
public static final String PRACTICE_NAME = "practiceName";
private long idPracticeConf;
private String practiceName;
// getters & setters...
}
NewPracticeController.java
public class NewPracticeController extends SimpleFormController{
protected SmartLogger log = SmartLogger.getLogger(this.getClass());
private PracticeSu practiceSu;
private ConfigurationSu configurationSu;
private HibernateEntityDataBinder practiceConfTBinder;
private HibernateEntityDataBinder practiceTBinder;
public NewPracticeController() {
setCommandClass(NewPracticeBean.class);
setCommandName("command");
}
#Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
log.trace("NewPracticeController -- initBinder");
super.initBinder(request, binder);
binder.registerCustomEditor(PracticeT.class, "practiceT", practiceTBinder);
binder.registerCustomEditor(PracticeConfT.class, "practiceT.practiceConfT", practiceConfTBinder);
}
#Override
protected Map referenceData(HttpServletRequest request) throws Exception {
log.trace("NewPracticeController -- referenceData");
Map model = new HashMap();
RetrieveAllEntitiesReq req = new RetrieveAllEntitiesReq();
req.setEntity(PracticeConfT.class);
req.setOrderProperty(PracticeConfT.PRACTICE_NAME);
RetrieveAllEntitiesResp resp = configurationSu.retrieveAllEntities(req);
List entitiesList = resp.getEntitiesList();
model.put("practiceTypeList", entitiesList);
return model;
}
#Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
NewPracticeBean practiceBean = (NewPracticeBean)command;
Map model = errors.getModel();
CreateNewPracticeReq req = new CreateNewPracticeReq();
req.setPracticeT(practiceBean.getPracticeT());
CreateNewPracticeResp resp = practiceSu.createNewPractice(req);
practiceBean.setPracticeT(resp.getPracticeT());
model.putAll(referenceData(null));
model.put(getCommandName(), practiceBean);
return new ModelAndView(getSuccessView(), model);
}
// setters and getters...
}
After spending some time with OptionsTag, OptionWriter and SelectValueComparator, I would say, then output of "selected" is based on Object.equals.
So if for any reason (Lazyloading...) the Object PracticeT.practiceConfT and the according Objects of model.put("practiceTypeList", entitiesList) are not the SAME (==) then forms:options will not select them as long as the equals method is not correct implemented.
So I guess you need to implement a correct equals method, even if this did not fix this problem, it is always better to have a correct equals method than a wrong or none.
Correct implemented means that it must pay attention to the fact that is used with Hibernate. (for example use if (Hibernate.getClass(this) != Hibernate.getClass(other)) instead of `if (this.getClass() != other.getClass() )

Categories