I am using spring MVC 3. I have been trying to access the attributes of the uploaded file but I keep getting the following error message. I can access the other fields of the form that is posted but I can't access the uploaded file.
nullhandleForm - Failed to convert property value of type 'java.lang.String' to required type
'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]
for property 'file': no matching editors or conversion strategy found
HTML/JSP file
<%#page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p>${Response}</p>
<h1>Upload Songs</h1>
<table>
<form:form action="" commandName="handleForm">
<tr>
<td>Song Name :</td>
<td><form:input path="songName"/></td>
</tr>
<tr>
<td>Artist Name :</td>
<td><form:input path="artistName"/></td>
</tr>
<tr>
<td>Gendre :</td>
<td><form:input path="gendre"/></td>
</tr>
<tr>
<td>description :</td>
<td><form:textarea path="description"/></td>
</tr>
<tr>
<td>Browse File :</td>
<td><form:input type="file" path="file" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="submit" /></td>
</tr>
</form:form>
</table>
</body>
The form handlerclass
public class HandleForm {
private String songName;
private String artistName;
private String gendre;
private String description;
private CommonsMultipartFile file;
public CommonsMultipartFile getFile() {
return file;
}
public void setFile(CommonsMultipartFile file) {
this.file = file;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGendre() {
return gendre;
}
public void setGendre(String gendre) {
this.gendre = gendre;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
}
The controller
#Controller
public class AdminController{
#RequestMapping(value = "/admin", method = RequestMethod.GET)
public String showAdmin(){
return "admin/index";
}
#RequestMapping(value = "/admin/upload-songs", method = RequestMethod.GET)
public String showContacts(Model model) {
model.addAttribute(new HandleForm());
return "/admin/upload";
}
#RequestMapping(value = "/admin/upload-songs", method = RequestMethod.POST)
public String doForm(#ModelAttribute(value = "handleForm") HandleForm handleForm, BindingResult result, Model model){
if(result.hasErrors()){
String stringList = null;
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
stringList += error.getObjectName() + " - " + error.getDefaultMessage() + "\n";
}
model.addAttribute("Response", stringList);
//model.addAttribute("songname", handleForm.getSongName()); works fine
//model.addAttribute("filename", handleForm.getFile().getOriginalFilename()); throws an error..?
}
return "/admin/upload";
}
}
Any help would be much appreciated. Thanks in advance
do you need to add the enctype to the form declaration?
enctype="multipart/form-data"
Also make sure you have the resolver for Multipart file in your dispatcher servlet
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
Related
JSP 1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Personal Info Page</title>
</head>
<body>
<center>
<h1>Personal Info Page</h1>
</center>
<center>
<form:form method="post" modelAttribute="pcbInfo"
action="/MyFirstWeb3/entrycontactinfo.do">
<table>
<tr>
<td>First Name:</td>
<td><form:input path="fName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lName" /></td>
</tr>
<tr>
<td>Middle Name:</td>
<td><form:input path="mName" /></td>
</tr>
<tr>
<td>Gender:</td>
<td><form:radiobutton path="gender" value="M" />Male
<form:radiobutton path="gender" value="F" />Female</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Next" /></td>
</tr>
</table>
</form:form>
</center>
</body>
</html>
JSP2
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contact Info Page</title>
</head>
<body>
<center>
<h1>Contact Info Page</h1>
</center>
<center>
<form:form method="post" modelAttribute="pcbInfo"
action="/MyFirstWeb3/entrybankinfo.do">
<table>
<tr>
<td>Address:</td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><form:input path="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><form:input path="state" /></td>
</tr>
<tr>
<td>Country:</td>
<td><form:input path="country" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><form:input path="phone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Next" /></td>
</tr>
</table>
</form:form>
</center>
</body>
</html>
JSP3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bank Info Page</title>
</head>
<body>
<center>
<h1>Bank Info Page</h1>
</center>
<center>
<form:form method="post" modelAttribute="pcbInfo"
action="/MyFirstWeb3/processaddpersoninfo.do">
<table>
<tr>
<td>Bank Name:</td>
<td><form:input path="bankName" /></td>
</tr>
<tr>
<td>Acc No:</td>
<td><form:input path="accNo" /></td>
</tr>
<tr>
<td>SSN:</td>
<td><form:input path="SSN" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</table>
</form:form>
</center>
</body>
#Controller
package com.apex.mfw.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.apex.mfw.bo.BankInfoBO;
import com.apex.mfw.vo.PCBInfo;
#Controller
#SessionAttributes({"pcbInfo"})
public class AddDataController {
BankInfoBO bankInfoBO;
#RequestMapping(value = "/entrypersonalinfo.do")
public ModelAndView entryPersonalInfo() {
System.out.println("Serving the Personalinfo form");
return new ModelAndView("personalinfo","pcbInfo", new PCBInfo());
}
#RequestMapping(value = "/entrycontactinfo.do")
public ModelAndView entryContactInfo(#ModelAttribute PCBInfo pcbInfo, HttpServletRequest req) {
System.out.println("Serving the contactinfo form");
return new ModelAndView("contactinfo","pcbInfo",pcbInfo);
}
#RequestMapping(value = "/entrybankinfo.do")
public ModelAndView entryBankInfo(#ModelAttribute PCBInfo pcbInfo) {
System.out.println("Serving the bankinfo form");
System.out.println("FNAME: " + pcbInfo.getfName());
System.out.println("MNAME: " + pcbInfo.getmName());
System.out.println("LNAME: " + pcbInfo.getlName());
System.out.println("GENDER: " + pcbInfo.getGender());
return new ModelAndView("bankinfo","pcbInfo",pcbInfo);
}
#RequestMapping(value = "/processaddpersoninfo.do")
public ModelAndView addBankInfo(#ModelAttribute PCBInfo pcbInfo, Model model) {
System.out.println("AddInfoController:addBankInfo():Start");
System.out.println("FNAME: " + pcbInfo.getfName());
System.out.println("MNAME: " + pcbInfo.getmName());
System.out.println("LNAME: " + pcbInfo.getlName());
System.out.println("GENDER: " + pcbInfo.getGender());
System.out.println("\n\nAddress: " + pcbInfo.getAddress());
System.out.println("City: " + pcbInfo.getCity());
System.out.println("State: " + pcbInfo.getState());
System.out.println("Country: " + pcbInfo.getCountry());
System.out.println("Phone: " + pcbInfo.getPhone());
bankInfoBO.addBankInfo(pcbInfo);
System.out.println("AddInfoController:addBankInfo():End");
return new ModelAndView("/MyFirstWeb3/entrypersonalinfo.do");
}
public AddDataController() {
}
}
VOBean
package com.apex.mfw.vo;
public class PCBInfo {
String fName;
String lName;
String mName;
String gender;
String address;
String country;
String city;
String state;
String phone;
String bankName;
String accNo;
String SSN;
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getAccNo() {
return accNo;
}
public void setAccNo(String accNo) {
this.accNo = accNo;
}
public String getSSN() {
return SSN;
}
public void setSSN(String sSN) {
SSN = sSN;
}
}
Here I am trying to traverse from JSP 1 to 2 to 3 and collect the data from the 3 pages in the above ValueObject bean
but the sys.out lines all print NULL values .. I am feeling pretty beat after trying several solutions on the internet. Help me find what am I doing wrong please.
I have a spring form with having backing object for it. The form is like this-
<sf:form cssClass="form-horizontal" commandName="campaignModel" method="post">
<sf:input path="campaign.name" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
</sf:form>
Model class(Form backing Object) -
CampaignModel.java
public class CampaignModel {
private Campaign campaign = new CampaignImpl();
private List<LandingPageModel> landingPageModels = new Arraylist<LandingPageModel>;
public Campaign getCampaign() {
return campaign;
}
public void setCampaign(Campaign campaign) {
this.campaign = campaign;
}
public List<LandingPageModel> getLandingPageModels() {
return landingPageModels;
}
public void setLandingPageModels(List<LandingPageModel> landingPageModels) {
this.landingPageModels = landingPageModels;
}
LandingPageModel.java is -
public class LandingPageModel {
private LandingPage landingPage = new LandingPageImpl();
private List<LandingPageParameterImpl> landingPageParameters = new ArrayList<LandingPageParameterImpl>();
public LandingPage getLandingPage() {
return landingPage;
}
public void setLandingPage(LandingPage landingPage) {
this.landingPage = landingPage;
}
public List<LandingPageParameterImpl> getLandingPageParameters() {
return landingPageParameters;
}
public void setLandingPageParameters(List<LandingPageParameterImpl> landingPageParameters) {
this.landingPageParameters = landingPageParameters;
}
}
LandingPage.java is -
public class LandingPageImpl extends EntityImpl implements LandingPage {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} }
So i want that i can insert many objects of landingPage (having their own url property) in landingPageModels list. That means i can have mulitple input tag having url property like this -
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
But when executing this code, spring gives me error that landingPage property of landingPageModels has not getter setter method. How to solve it and how to take multiple value like this ?
In order to bind a list model property to multiple input fields, you need this in the rendered form:
<input type="text" name="landingPageModels[0].landingPage.url" class="form-control" />
<input type="text" name="landingPageModels[1].landingPage.url" class="form-control" />
<input type="text" name="landingPageModels[2].landingPage.url" class="form-control" />
Which is accomplished by:
<c:forEach items="${campaignModel.landingPageModels}" varStatus="s">
<sf:input path="landingPageModels[${s.index}].landingPage.url" class="form-control" />
</c:forEach>
The error you are getting is correct as landingPageModels is a list.
You would need to use index access like this landingPageModels[0].landingPage.url.
If you have dynamic number of input/url, then you can use <c:forEach> to create dynamic input variable names
<c:forEach items="${campaignModel.landingPageModels}" var="landingPage">
<sf:input path="landingPage.url" class="form- control" />
</c:forEach>
Will the above not works for you to view data? To get them in controller you may have to use dynamic table row concept in HTML or for each single LandingPage entry add to form bean object by clicking add button and render back.
In my case Person command object having List<Token> property, in order to bind the list of tokens we have designed page as attached screen shot below, clicking on Add button hits the controller and add the each token List<Token> and render back to same view and displays added token in list view, it facilitates to add multiple token for Person.
I dont know how to do it with spring form lib input but if you want to bind using simple html input than you can bind list like this
Simple.jsp
<%#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"%>
<html>
<head>
</head>
<body>
<form:form method="post" action="save.html" modelAttribute="contactForm">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td><input name="contacts[0].firstname" /></td>
<td><input name="contacts[0].lastname" /></td>
</tr>
<tr>
<td><input name="contacts[1].firstname" /></td>
<td><input name="contacts[1].lastname" /></td>
</tr>
</table>
<br/>
<input type="submit" value="Save" />
</form:form>
</body>
</html>
#controller :
#RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(#ModelAttribute("contactForm") ContactForm contactForm) {
List<Contact> contacts = contactForm.getContacts();
if(null != contacts && contacts.size() > 0) {
ContactController.contacts = contacts;
for (Contact contact : contacts) {
System.out.printf("%s \t %s \n", contact.getFirstname(), contact.getLastname());
}
}
return new ModelAndView("show_contact", "contactForm", contactForm);
}
ContactForm.java
import java.util.List;
public class ContactForm {
private List<Contact> contacts;
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
Contact.java
public class Contact {
private String firstname;
private String lastname;
private String email;
private String phone;
public Contact() {
}
//getters and setters
}
This is my model class
public class DynamicRow {
private String id;
private String name;
private String email;
public DynamicRow(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public DynamicRow() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "DynamicRow [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
This is the form class
public class DynamicRowForm {
private List<DynamicRow> dynamicRow = LazyList.decorate(new ArrayList<DynamicRow>(), FactoryUtils.instantiateFactory(DynamicRow.class));
public DynamicRowForm() {
}
public List<DynamicRow> getDynamicRow() {
return dynamicRow;
}
public void setDynamicRow(List<DynamicRow> dynamicRow) {
this.dynamicRow = dynamicRow;
}
}
Below is my controllers
#RequestMapping(value="/createDetails")
public String list(Model model){
DynamicRowForm dynamicRowForm = new DynamicRowForm();
model.addAttribute("DynamicRowForm",dynamicRowForm);
return "test2";
}
#RequestMapping(value="/save")
public String showList(Model model,#ModelAttribute("DynamicRowForm") DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow());
return "success";
}
And this is my jsp page named as test2
<%#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>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;
function addMoreRows(form) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%" maxlength="120" /></td><td><input name="" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> Delete</p>';
$('#addedRows').append(recRow);
}
function removeRow(removeNum) {
$('#rowCount'+removeNum).remove();
}
</script>
</head>
<body>
<form:form action="save" method="GET" modelAttribute="DynamicRowForm">
<input type="button" onclick="addMoreRows(this.form);" value="AddRow">
<table rules="all" style="background:#fff;">
<tr>
<td style="font-size:14px;" >Name</td>
<td style="font-size:14px;">Email</td>
<td style="font-size:14px;">Mobile</td>
<!-- <td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
Add More
</span>
</td> -->
</tr>
<tr id="rowId">
<td><form:input path="${dynamicRow.id}" type="" size="17%"/></td>
<td><form:input path="${dynamicRow.name}" type="text" /></td>
<td><form:input path="${dynamicRow.email}" type="text" /></td>
</table>
<div id="addedRows"></div>
<input type="submit" value="Save">
</form:form>
</body>
I know these questions have been asked before, but as am very new to spring mvc and learning things so am trying to get some comfort in this technology..
Anyway, what here I am trying to do is to save multiple row/list of object... but its not getting saved in the list... Please help me out on what is the mistake am doing, and correct me.
Edit
And one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using #ModelAttribute with same name..but here am getting different object.why?
Your inputs need to look like this:
<form:form action="save" method="POST" modelAttribute="DynamicRowForm">
...
<td><form:input path="dynamicRow[0].id" type="text" /></td>
<td><form:input path="dynamicRow[0].name" type="text" /></td>
<td><form:input path="dynamicRow[0].email" type="text" /></td>
...
</form:form>
Which essentially states: on form submission bind the id field to the DynamicRow at index 0 in the form backing Object - DynamicRowForm.
If you do this, then the following should print the values input:
#RequestMapping(value="/save")
public String showList(#ModelAttribute DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow().get(0).getId());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getName());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getEmail());
return "success";
}
And one more thing I would like to clarify, in my first controller am
creating a dynamicRowForm object and adding into model so that my jsp
page can access it...and in my second controller am receiving
DynamicRowForm object using #ModelAttribute with same name..but here
am getting different object.why?
Because it is stateless. If you want to work with the same instance you will need to store it in the session between requests. See the following useful discussion.
http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/
You are missing indexing here as you are using collection. Try...
<form:input path="${DynamicRowForm.dynamicRow[0].id}" type="" size="17%"/>
and also in your addRow(), you have to use the dynamic index with row count. If it is not working with form:input, try simple html input type.
var count=0;
function addMoreRows(form) {
rowCount ++;
count =++count;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input path="users['+count+'].id" id="users'+count+'.id" name="users['+count+'].id" type="" size="17%" maxlength="120" /></td><td><input path="users['+count+'].name" id="users'+count+'.name" name="users['+count+'].name" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input path="users['+count+'].email" id="users'+count+'.email" name="users['+count+'].email" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> Delete</p>';
$('#addedRows').append(recRow);
}
I am new to ATG. And I am trying to use RepositoryFormHandler of my own. But I am not able to do the validations on the form.
Here is my .java file:
public class MyLoginBean extends RepositoryFormHandler {
private String logname;
private String logpwd;
private String message;
public String getLogname() {
return logname;
}
public void setLogname(String logname) {
this.logname = logname;
}
public String getLogpwd() {
return logpwd;
}
public void setLogpwd(String logpwd) {
this.logpwd = logpwd;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean handleLogname(DynamoHttpServletRequest pRequest,
DynamoHttpServletResponse pResponse) throws ServletException,
IOException {
boolean tf=true;
if(logname.isEmpty() || logname==null)
{
tf=false;
setMessage("User name can't empty");
}
System.out.println("inside logname");
return tf;
}
public void handleFormException(DropletFormException exception,
DynamoHttpServletRequest request, DynamoHttpServletResponse response) {
// TODO Auto-generated method stub
super.handleFormException(exception, request, response);
}
}
And here is my .jsp file:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/dspTaglib" prefix="dsp" %>
<dsp:importbean bean="/atg/dynamo/droplet/ErrorMessageForEach"/>
<dsp:importbean bean="/dynamusic/MyLoginBean"/>
<!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>Custom Login</title>
</head>
<body>
<dsp:form style="color:white">
<table style="background:#3b5998">
<tr>
<td>
<ul>
<dsp:droplet name="ErrorMessageForEach">
<dsp:param bean="MyLoginBean.formExceptions" name="exceptions"/>
<dsp:oparam name="output">
<li>
<dsp:valueof param="message"/>
</li>
</dsp:oparam>
</dsp:droplet>
</ul>
</td>
</tr>
<tr>
<td>
User Name:
</td>
<td>
Password:
</td>
</tr>
<tr>
<td>
<dsp:input type="text" name="logname" bean="MyLoginBean.logname"> </dsp:input>
</td>
<td>
<dsp:input type="password" name="logpwd" bean="MyLoginBean.logpwd"> </dsp:input>
</td>
<td>
<dsp:input type="submit" bean="MyLoginBean.login"> </dsp:input>
</td>
</tr>
</table>
</dsp:form>
</body>
</html>
This is all I've tried so far and still trying something else.
Please suggest the solution for it and also tell me the mistakes, if any, in the code pasted here.
Don't override handleFormException
Instead of using setMessage, use ATG's built-in behavior. All form handlers inherit a Vector of form exceptions from the GenericFormHandler superclass. To add an error, use:
addFormException(new DropletException("Your error message"));
Then, at the end of your method, call:
return checkFormRedirect(getSuccessUrl(), getFailUrl(), pRequest, pResponse);
This checks if any form exceptions have been added, and if so, redirects to the failUrl, otherwise redirects to the successUrl.
By convention, you should name your form handler *FormHandler, for example ProfileFormHandler, BillingInfoFormHandler, PaymentInfoFormHandler etc.
Hope this helps. See http://docs.oracle.com/cd/E22630_01/Platform.1002/apidoc/atg/droplet/GenericFormHandler.html#getFormExceptions()
I am quite new to spring framework and I'm stuck with the following issues:
I am trying to insert multiple records on a single post request using Spring MVC 3.0
I successfully bound the List object and it is populating on JSP and when I submit the
form the request is reaching on the controller method(post) but the returned object does not contain proper values, its printing null.
My code is as follows:
form.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:form commandName="teamBean" method="post">
<f:input path="players[0].fname" />
<f:input path="players[0].lname" />
<f:input path="players[0].phone" />
<f:input path="players[0].email" />
<input type="submit" value="submit" />
</f:form>
</body>
</html>
DynaminFormController.java
#Controller
#RequestMapping("/form")
public class DynaminFormController {
List<Player> players = new ArrayList<>();
#RequestMapping(method = RequestMethod.GET)
public String getForm(Map<String, TeamBean> map) {
TeamBean teamBean = new TeamBean();
players.add(new Player("dd", "dd", "dd", "dd"));
players.add(new Player("cc", "cc", "cc", "cc"));
teamBean.setPlayers(players);
map.put("teamBean", teamBean);
return "form";
}
#RequestMapping(method = RequestMethod.POST)
public String postForm(TeamBean teamBean) {
System.out.println("DynaminFormController.postForm()");
System.out.println(teamBean);//printing null
return "view";
}
}
TeamBean.java
public class TeamBean {
private List<Player> players;
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
#Override
public String toString() {
return "TeamBean [players=" + players + "]";
}
}
Player.java
public class Player {
private String fname;
private String lname;
private String phone;
private String email;
public Player(String fname, String lname, String phone, String email) {
this.fname = fname;
this.lname = lname;
this.phone = phone;
this.email = email;
}
///getters setters...
#Override
public String toString() {
return "Player [fname=" + fname + ", lname=" + lname + ", phone="
+ phone + ", email=" + email + "]";
}
}
You need #ModelAttribute on you Post Method
#RequestMapping(method = RequestMethod.POST)
public String postForm(#ModelAttribute("teamBean") TeamBean teamBean) {
System.out.println("DynaminFormController.postForm()");
System.out.println(teamBean);//printing null
return "view";
}
I have resolved my issue, the problem was with spring's input tag ,i don't know why it was not working but just replace the f:input with html's input tag,though u may not get the populated values from controller but it's fine.
<f:form commandName="teamBean" method="post">
<tr>
<td><input class="dy" name="players[0].fname" /></td>
<td><input class="dy" name="players[0].lname" /></td>
<td><input class="dy" name="players[0].phone" /></td>
<td><input class="dy" name="players[0].email" /></td>
</tr>
<input type="submit" value="submit" />
</f:form>