I used angular in my jsp page but I'm confused how to bind values to inputbox
Here My is my Controller
#RequestMapping(value="/customer" , method = RequestMethod.GET)
public String getCustomerPage(Model model,HttpSession session){
CustomerBean customerBean = getCustomerById(id);
model.addAttribute("customer", customerBean);
return "addCustomer"
}
My jsp code
<input type="text" name="customerName" value="${customer.name}" ng-model="cust.name" required/>
Unable to bind value
But if i removed ng-model then value bind to control
i.e
<input type="text" name="customerName" value="${customer.name}" required/>
this works
Help!
Related
How to send data from jsp to controller in spring using form ?
<form:form action="${searchUrl}" method="post">
<div class="form-group">
<label class="control-label"> search by ID </label>
<br>
<input type="text" id="ticketId" placeholder=" Enter ticket ID ">
</div>
</form:form>
Could not able to get ticketId using this method
You need to declare name attribute (name="ticketId") and access the same on the controller. Like below.
JSP:
<input type="text" name="ticketId" id="ticketId" placeholder=" Enter ticket ID">
Controller:
#RequestParam(value = "ticketId", required = false) String ticketId
I hope it is helpful to you, for me it is working fine.
If you want to use spring forms, make sure to follow the below steps :
Step:1.In the spring controller, you should return the bean object like below to respected JSP.
Class User {
private String ticketId;
// setter & getter
}
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String init(Model model) {
model.addAttribute("msg", "Please Enter Your Login Details");
model.addAttribute("loginBean", new User());
return "login";
}
Step:2 add a model attribute and add taglib in the JSP page.
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<form:form action="${searchUrl}" method="post"
modelAttribute="loginBean">
<div class="form-group">
<label class="control-label"> search by ID </label>
<br>
<form:input type="text" id="ticketId" path = "ticketId"
placeholder=" Enter ticket ID " />
</div>
</form:form>
Step : 3
#RequestMapping(value = "/test", method = RequestMethod.Post)
public String init(Model model,
#ModelAttribute("user") User user,BindingResult
result) {
sout("user"+user);
return "home";
}
I migrated a Spring 4.1 application (running I JBoss) to SpringBoot 1.5 (Spring 4.3.7), and after that, all our MVC Controllers with String parameters always receive null values when called.
The only way I could get the parameter value set was to replace String with StringBuilder.
An example, not working anymore:
#RequestMapping(value = "/updateTime", method = RequestMethod.POST)
public String updateTime(#RequestParam(value="time") String time) {
// time is null in SpringBoot
}
Working (StringBuffer instead of String:
#RequestMapping(value = "/updateTime", method = RequestMethod.POST)
public String updateTime(#RequestParam(value="time") StringBuffer time) {
// time has expected value
}
HTML:
<form name="updateTime" action="updateTime.html" method="post">
Time: <input type="text" name="time" placeholder="YYYY-MM-DD HH:mm" value="${time}"/> <br/>
<input type="submit" name="action" value=" Update "/>
</form>
Even when String parameters in the controller are compounded into a request object I get this behaviour.
Am I doing something wrong here?
INFO : Spring Works with HTML name attributes.
You should have the same name and RequestParam value.
Note : in the HTML form action it's better to have the same as the RequestMapping value also.
Try this :
<form name="updateTime" action="/updateTime" method="post">
Time: <input type="text" name="time" placeholder="YYYY-MM-DD HH:mm" value="${time}"/> <br/>
<input type="submit" name="action" value=" Update "/>
</form>
#RequestMapping(value = "/updateTime", method = RequestMethod.POST)
public String updateTime(#RequestParam(value="time") String time) {
// time is null in SpringBoot
}
I currently have the following method within my controller that takes the users form input and passes it to an SQL query to return a list of matches.
#RequestMapping(value = "/resultsPage", method = RequestMethod.GET)
public ModelAndView newSearch(HttpServletRequest request)
{
int id = Integer.parseInt(request.getParameter("id"));
List<newobject> listSearch = newDAO.loadSearch(id);
ModelAndView model = new ModelAndView("results");
model.addObject("listSearch", listSearch);
return model;
}
I have added #NotNull to the ID filed in the newObject however I am unsure on how to modify my method above to check the variable entered (or not entered) by the user.
I am also unsure how to display the error on the html page. My existing code is below:
<form method="get" th:action="#{/results}">
<input id="search" name="id" class="search" placeholder="Ref..."
type="text" maxlength="10" title="Numerical values only" />
<button type="submit" method="post" style="display:none;" id="search">Search</button>
</form>
Can anyone give me some advice on how I would add the validation as I am just getting errors with everything I try.
I started to working on thymeleaf, so after submitting the form I end up getting this URL:
URL
http://localhost:8080/submit?name=xyz&age=20&dropdown=male
so how should I get the value of individual elements to java controller?
In java part, I should be able to load them into their respective datatypes.
To grab GET parameters (URL parameters), use request.getParameter
String name = request.getParameter("name");
String age = request.getParameter("age");
String dropdown = request.getParameter("dropdown");
Note that these are all strings, if you want other datatypes you will need to parse them and handle any errors.
So, yes later I was able to solve it
Step1Create an HTML plage GetDetails.HTML
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Details</title>
</head>
<body>
<form action="#" th:action="#{PATH OF ACTION}" method="get" th:object="${details}">
<input type="text" name="name" th:field="*{name}"/>
<input type="text" name="age" th:field="*{age}"/>
<select name="option" th:field="*{option}">
<option value="male">male</option>
<option value="female">female</option>
</select>
<input type="submit" value="search"/>
</form>
</body>
</html>
Step2 Create a Model class in java (POJO)
class Details{
private String name;
private int age;
//getters and setters
//constructors
}
NOTE: Class name must be same as the object name given in HTML page
Step3 Controller part in Java
#RequestMapping("/")
public String search(Model model){
model.addAttribute("details", new Details());
return "GetDetails";
}
Step4 REST controller
#GetMapping(value = "PATH OF ACTION")
public List<Details> getAll(#ModelAttribute Details details) {
// Business service
}
i am working with Spring where my form fields are same with attribute fields so when i submit form it directly maps to database fields and save the data it works perfectly, but what if i want to save multiple objects with one form,
HTML:
<form>
Payment:<br>
<input type="text" name="payment"><br>
Date:<br>
<input type="date" name="paymentDate">
</form>
POJO:
public class ProjectPayment
{
private Double payment;
private Date paymentDate;
// setters and getters
}
Controller:
#RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
public #ResponseBody String SaveProject(ProjectPayment projectPayment) {
projectPaymentService.saveProjectPayment( projectPayment);
}
this works perfectly,
but now in my some scenario i need multiple objects dynamically then how to save in database, how controller should look like
for example:
Now my Form is
<form>
Payment:<br>
<input type="text" name="payment"><br>
Date:<br>
<input type="date" name="paymentDate">
Payment:<br>
<input type="text" name="payment"><br>
Date:<br>
<input type="date" name="paymentDate">
Payment:<br>
<input type="text" name="payment"><br>
Date:<br>
<input type="date" name="paymentDate">
Payment:<br>
<input type="text" name="payment"><br>
Date:<br>
<input type="date" name="paymentDate">
</form>
Now this form have multiple objects of ProjectPayment class but it saves only one object
please tell me how my controller should like, i have done like this but it occurs exception
Controller:
#RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
public #ResponseBody String SaveProject(ProjectPayment[] projectPayment) {
for(ProjectPayment propay : projectPayment)
{
projectPaymentService.saveProjectPayment( propay );
}
}
I can understand that you want to post data from a grid/table, however it's too ambiguous to determine which field map to which object.
Example:
field1
field2
field3 ==>Map to object at index 1 or 2?
filed1
So you think field3 should map to array index=1 or index=2?
So I suggest you should submit one by one to solve this issue.
Simple way to solve this problem is create a ViewModel.
e.g.
public class ProjectPaymentViewModel
{
private List<ProjectPayment> listProjectPayment;
// setters and getters
}
Use this view model on web page and controller
<form>
Payment:<br>
<input type="text" name="listProjectPayment[0].payment"><br>
Date:<br>
<input type="date" name="listProjectPayment[0].paymentDate">
Payment:<br>
<input type="text" name="listProjectPayment[1].payment"><br>
Date:<br>
<input type="date" name="listProjectPayment[1].paymentDate">
Payment:<br>
</form>
On controller
#RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
public #ResponseBody String SaveProject(ProjectPaymentViewModel projectPaymentViewModel) {
for(ProjectPayment propay : projectPaymentViewModel.getListProjectPayment())
{
projectPaymentService.saveProjectPayment( propay );
}
}