I am trying to get the value of a form from a jsp to use in a function in the controller and display another jsp
<form action="/uc">
<input name="cnp" type="text">
<br>
<br>
<input type="submit" value="Find">
</form>
This one is my Controller method
#RequestMapping(value = "/uc", method = RequestMethod.GET)
public String userContracts(#RequestParam("cnp") String cnp, Model model)
{
List<Contract> ContractList = new ArrayList<Contract>();
ContractList = cl.getContractsOfUser(cnp);
model.addAttribute("ContractList", ContractList);
System.out.println("In uc");
return "UserContracts";
}
Thanks pedram ezzati for the help!
The problem was that I had to use
<form action="http://localhost:8080/SpringWebTemplate/uc.html">
My previous attempts where either without the .html or just using /uc
Make sure your package is scan while spring come up.
In app-config.xml file check below tag
context:component-scan base-package="com.test.ashok"
Related
I have a html page with Thymeleaf and i have a button in that page. That button it is calling a method from the controller that i have mapped with /addService but it return the /index page(Because I'm already in that page and i want to remain in it). Therefore, when i press the button it is redirecting me to the index page but the URL is changed with /addService. I would like to remain in the index page without the /assService in the URL. Below my code of the form and of the controller. How can I avoid this behaviour?
Thanks in advance.
My index.html
<form action="#" th:object="${serviceOffered}" th:action="#{addService}" method="POST">
<input type="hidden" th:value="${serviceOffered.name}" name="name"/>
<button type="submit" class="btn">
<i class="fas fa-plus"></i>
</button>
</form>
My controller
#RequestMapping(value="addService", method = RequestMethod.GET)
public ModelAndView getServiceSelected() {
ModelAndView modelAndView = new ModelAndView();
ServiceOffered serviceOffered = new ServiceOffered();
modelAndView.addObject("serviceOffered", serviceOffered);
modelAndView.setViewName("/index");
return modelAndView;
}
Use redirect
modelAndView.setViewName("redirect:/");
And define your url "/"
#GetMapping("/")
public String indexPage() {
return "index":
}
Assuming that you use Spring MVC you should change your RequestMethod to POST, because you're POSTing a form. Otherwise the request is not handled correctly.
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'm a trainee java-dev and this is my first question here so please, don't judge me!
I have a Controller class which works with jsp files. The first jsp (userinput.jsp) has 3 text fields (lat, lon, radius) and 2 buttons (submit, apply default values). The second jsp is just an HTML table filled with data (depends on user input), and a reset button which should return you to the starter page (userinput.jsp) and delete all existing data. How should I do this?
Bonus question: If I try to refresh the page at the second state (html table), the browser generates a warning that says I'll lose all data and I shouldn't refresh. How can I get rid of this?
#Controller
#EnableAutoConfiguration
class SpringBootController implements InitLogger {
#GetMapping(value="/geohash")
public String getUserInput(ModelMap model) {
model.put("command", new Tuple());
return "UserInput";
}
#PostMapping(value="/geohash", params="SubmitWithDefault")
public String defaultUserInput(ModelMap model) {
model.put("command", tupleFill (48.104564, 20.800041, 6) );
return "UserInput";
}
#PostMapping(value = "/geohash", params="Submit")
public String printHash(#ModelAttribute("user")Tuple tuple,ModelMap model) {
GetData.setLat1(tuple.getFirstCoordinate());
GetData.setLon1(tuple.getSecondCoordinate());
GetData.setRad1(tuple.getRadius());
LocationExecute.calculate();
model.addAttribute("geoItemList", LocationExecute.getTupleList());
model.addAttribute("listSize", LocationExecute.getTupleList().size());
return "Geohash";
}
#PostMapping(value = "/geohash", params="reset", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:geohash");
}
}
userinput.jsp - buttons
<input type="submit" name="Submit" value="Submit" style="height:25px; width:100px"/>
<input type="submit" name="SubmitWithDefault" value="Default Values" style="height:25px; width:100px">
geohash.jsp - (html table) reset button
<input type="reset" name="reset" value="Reset" style="height:30px; width:100px">
You need to implement PRG(Post -Redirect-Get) design pattern in MVC to solve the issue.
Please go through the below for more information
http://www.c-sharpcorner.com/UploadFile/dacca2/implement-prg-pattern-in-mvc-architecture/
So instead of returning view name redirect it so your problem will be solved, if you want some data to be sent to redirect method then use flashAttributes of spring
Solved it this way:
geohash.jsp
(added form:form tags)
<form:form action="/geohash">
<th> <input type="submit" name="reset" value="Reset" style="height:30px; width:100px"> </th>
</form:form>
Controller
(changed method to post)
#RequestMapping(value = "/geohash", params="reset", method = RequestMethod.POST)
public ModelAndView method() {
return new ModelAndView("redirect:geohash");
}
i have a few questions to jsp and spring mvc..
I need get id value from jsp form and set in url for send request on /request/{id}
My jsp page:
<form action="/get-by-id/{id}" method="post">
<input type="text" name="id">
<input type="submit" name="Submit">
</form>
and code controller for 3 question:
#RequestMapping(value = "/change-pupil/{id}", method = RequestMethod.GET)
public String changePupil(#PathVariable("id") int id, #ModelAttribute Pupil pupils){
System.out.println(id + " " + pupils.toString());
return "redirect:/main";
}
#RequestMapping(value = {"/","/main"}, method = RequestMethod.GET)
public String index(){
return "index";
}
how to get value from input and install it in the url?
I create method in controller which get id and send this id to next page and from next page send request with id in url to controller, but this method very bad
how move from page to page without using a controller methods?
How return main page without index()?
Thanks for the help!
I am writing web-app using Spring MVC. As a view I use JSP. I want to bind data from spring tags and params in controller method. F.e. I have a add user form:
<form:form action="/add_user" commandName="user">
<div>
First name:<form:input path="firstName"/><br>
Last name:<form:textarea path="lastName"/>
</div>
<div>
<input type="submit" value="Add user">
</div>
</form:form>
and a controller:
#Controller
#RequestMapping(value = "/")
public class MainController {
#Autowired
private UserService userService;
...
#RequestMapping(value = "/add_user", method = RequestMethod.POST)
public String addUser(??? User newUser ????) {
userService.add(newUser);
return "redirect:/users";
}
...
}
What should I put instead of ??????? between the brackets?
I would be overjoy if you give me detaile tutorial or plain documentation (not "http://docs.spring.io/")
Follow micha's answer to make your code working. For more details and basic concepts about the form submission and displaying the value from Controller to jsp page you can look at the tutorials http://www.codejava.net/frameworks/spring/spring-mvc-form-handling-tutorial-and-example.
You need to add #ModelAttribute to your newUser parameter:
#RequestMapping(value = "/add_user", method = RequestMethod.POST)
public String addUser(#ModelAttribute("user") User newUser) {
..
}
Have a look at the form submission guide. This guide uses Thymeleaf instead of JSP but the form handling inside the controller is the same.