I have made a text field and a submit button in my view in the admin page, and i want to do so the text i submit is shown below my textbox on the same page.
This is my controller for getting to the adminPage:
#RequestMapping(value = "/adminPage", method = RequestMethod.POST)
public String adminPage(Model model) {
return "adminPage";
}
this is what i have for my adminPage:
<form th:action="#{/adminPage}" method="post">
<textarea rows="4" cols="50">
</textarea>
<input type="submit" class="btn btn-lg btn-primary btn-block"
value="Submit Text"/>
</form>
I'm still very new at controllers and MVC in general, and i find it hard to use my knowledge in Java because Controllers doesn't look like any Java i've used before, so any help would be appreciated!
ok then, no Javascript. First, textarea needs a name attribute name="inputText".
That name will be used in the model object when your server method receives the request:
#RequestMapping(value = "/adminPage", method = RequestMethod.POST)
public String adminPage(#RequestParam("inputText") String input, Model model) {
//Do stuff
model.addAttribute("theText", input); //add the text which can be accessed on "adminPage"
return "adminPage";
}
then you can add a <div> in "adminPage.jsp" and append your text there, like:
<div>${theText}</div>
If, I have understood your problem correctly than here is the workaround that will do the job.
When user visits /adinPage from browser than input_data variable will be null and the if condition won't be executed.
The JSP page will be returned with second textarea as blank.
You have to use JSP because HTML pages can't be altered.
Controller.java
#RequestMapping(value = "/adminPage", method = RequestMethod.POST)
public String adminPage(#RequestParam(value = "input_data", required = false) String input_data,Model model)
{
if(input_data!=null)
model.addAttribute("output_data",input_data);
return "adminPage";
}
adminPage.jsp
<form th:action="#{/adminPage}" method="post">
<textarea id="input_data" rows="4" cols="50">
</textarea>
<textarea rows="4" cols="50">
${output_data}
</textarea>
<input type="submit" class="btn btn-lg btn-primary btn-block"
value="Submit Text"/>
</form>
When the user submits the form, the if condition will be executed and the returned view will contain the previously input data in second textarea
I haven't tested the code so, it might contain some syntax errors.
Related
The code works nice. But the exercise says to do it with "links" and so I would want a way to do it with a link and not with a submit button/form. Is there a way?
Index page:
<form action="/resultaat/groterdan" method="post">
<label for="groterdan">Lijst met betalingen groter dan: </label><input type="text"
name="groterdan" id="groterdan"><input
type="submit" value="Parameter Verzenden">
</form>
Controller:
#RequestMapping("/resultaat/groterdan")
public String groterdan(Model model, HttpServletRequest request){
Double referentie = Double.valueOf(request.getParameter("groterdan"));
List<Betaling> betalingsLijst = betaalRepository.geefLijstBetalingGroterDan(referentie);
model.addAttribute("betalingsLijst",betalingsLijst);
return "resultaat";
}
Repository
#Query("SELECT b from Betaling b where b.bedrag > :referentie")
List<Betaling> geefLijstBetalingGroterDan(Double referentie);
I am having a small issue with Spring Boot forms displaying the information of the path value instead of the placeholder once you get to the editProfile.jsp. I want the input field to look like this;
Edit Profile Page instead of this Wrong Edit Profile. I do not want my users to have to click, select and delete the auto completed value. I want it to show the placeholder only and allow them to overwrite what is shown with ease.
This is the editProfile.jsp
<%--#elvariable id="editProfile" type=""--%>
<form:form method="POST" modelAttribute="editProfile">
<div class="MyForm form-group">
<h1>Edit Profile</h1>
<form:input type="email" class="MyInput" id="email" path="email" placeholder="${editProfile.email}" />
<form:button type="submit" class="from-control">Submit</form:button>
</div>
<div>
<img src="images/reg1.png" alt="picture">
</div>
</form:form>
</body>
</html>
This is the code specified in the Controller
#RequestMapping(value = "edit/{email}", method = RequestMethod.GET)
public String getEditUserData(#PathVariable("email") String email, Model model) {
AccountEntity accountInstance = accountRepo.findByEmail(email);
model.addAttribute("editProfile", accountInstance);
return "editProfile";
}
#RequestMapping(value = "edit/{email}", method = RequestMethod.POST)
public String enterEditUserData(#ModelAttribute("login") AccountEntity accountForm, #PathVariable("email") String email, Model model ) {
AccountEntity accountInstance = accountRepo.findByEmail(email);
accountInstance.setEmail(accountForm.getEmail());
accountRepo.save(accountInstance);
return "redirect:/login";
}
I have figured it out; You have to add a model of a new Entity, so the path variable does not fill in with the instance of the specific path value. Here is the new code, and compare it to the one I sent above.
#RequestMapping(value = "edit/{email}", method = RequestMethod.GET)
public String getEditUserData(#PathVariable("email") String email, Model model) {
AccountEntity accountInstance = accountRepo.findByEmail(email);
model.addAttribute("editProfile2", new AccountEntity());
model.addAttribute("editProfile1", accountInstance);
return "editProfile";
}
<%--#elvariable id="editProfile" type=""--%>
<%--#elvariable id="editProfile2" type=""--%>
<form:form method="POST" modelAttribute="editProfile2">
<div class="grid form-group">
<h1>Edit Profile</h1>
<form:input type="email" class="MyInput" id="email" path="email" placeholder='${editProfile1.email}' />
<form:button type="submit" class="from-control">Submit</form:button>
</div>
Hi I'm new with Spring and I'm having problems in passing data between two pages using two different controllers.
I would like to know how I can handle this situations.
In my index.html I have a button that should redirect me to a new page passing some data. When i click the button it redirects me to the step2 page but I don't have to objects. How can I solve this? Is the GET method correct? Do I have to use the form just for passing some data between pages and controllers?
Below is what I have.
Index.html
<form th:action="#{/step2}" method="GET">
<input type="hidden" th:value="${mapSelectedServices}" name="mapSelectedServices"/>
<input type="hidden" th:value="${user}" name="loggedUser"/>
<div class="form-group d-flex align-items-center justify-content-between">
<button type="submit" class="btn btn-danger btn-rounded ml-auto" >SEE PRICES
<i class="fas fa-long-arrow-alt-right ml-2"></i>
</button>
</div>
</form>
Step2Controller
#RequestMapping(value="step2", method = RequestMethod.GET)
public ModelAndView step2(ModelAndView modelAndView, #ModelAttribute("user") User user,
#ModelAttribute("mapSelectedServices") HashMap<String,List<ServiceOffered>> mapSelectedServices,
BindingResult bindingResult){
modelAndView.addObject("user", user);
modelAndView.addObject("mapSelectedServices", mapSelectedServices);
modelAndView.setViewName("step2");
return modelAndView;
}
Sorry for all the questions, but I'm new to spring development.
HTML page:
<form th:action="#{/step2}" method="POST">
<input type="hidden" th:value="${mapSelectedServices}" name="mapSelectedServices"/>
<input type="hidden" th:value="${user}" name="loggedUser"/>
<div class="form-group d-flex align-items-center justify-content-between">
<button type="submit" class="btn btn-danger btn-rounded ml-auto" >SEE PRICES
<i class="fas fa-long-arrow-alt-right ml-2"></i>
</button>
</div>
</form>
Controller method:
public ModelAndView goToPgae2(#ModelAttribute ModelClass aClass)
{
ModelAndView mv=new ModelAndView("SecondHtlmPageName");//setting view name here
mv.addAttribute("aClass",aClass);
return mv;
}
Model Class with the specific variables passed from one page to another:
class ModelClass {
public Stirng mapSelectedServices; //use appropriate data type.
public String loggedUser;
//create getters and setters
}
Second page
<div>${aClass.loggedUser}</div>
DONE.
This way you can go to second page . And if you want to redirect to second page and the model attributes should be available there then you need to use flashattribute.
So I'm having this code in a JSP file:
<form action="/demo/quests/view?questId=${m.id}" >
<button type="submit" class="btn btn-primary" >See quest</button>
</form
My method has this definition:
RequestMapping(value = "/view", method = RequestMethod.GET)
public String displayQuest(#RequestParam(value = "questId") String questId, Model model){}
The problem is that my form is redirrecting to quests/view?
How can I make it redirrect to quests/view?questId=asdasdasd
**Using #requestParam is a must, because with #PathVariable I get a weird bug
Your method definition is ok but you should use your form submission as :
<form action="${pageContext.request.contextPath}/demo/quests/view">
<input type="text" class="form-control" id="questId"
name="questId" required>
<button type="submit" class="btn btn-primary" >See quest</button>
</form
#RequestParam handles that there is a parameter with name questId which is described at your #RequestParam(value = "questId") method signature. You can check an exact example from my github repo.
Form Sample: modalAuthor.jsp#form
Handler Method Sample AuthorController#authorPost
Replace
<form action="/demo/quests/view?questId=${m.id}" >
with
<form action="view?questId=${m.id}" >
I am trying to submit a form to controller in using java spring, in following code i am retrieving file element by following way successfully but not getting how to retrieve other elements(shortname and full name)value.
please help me out.
<body>
<div style="text-align: center; margin-top: 60px;">
<form action="upload" enctype="multipart/form-data">
<input type="hidden" id="shortName" name="michael">
<input type="hidden" id="fullName" name="michael jackson">
Select file:
<input type="file" name="dataFile" id="fileAttachment"/><br/><br/>
<div style="text-align: center; margin-top: 100px;">
<input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" />
</div>
</form>
</div>
</body>
Controller side code :
#RequestMapping(value = "upload", method=RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response,
#RequestPart("dataFile") MultipartFile file
){
System.out.println(file.getSize());
}
first change the input elements and create the name attribute for both shortName and fullName like so :
<input type="hidden" id="shortNameId" name="shortName" value="michael">
<input type="hidden" id="fullNameId" name="fullName" value="michael jackson">
however you can remove the default value attribute and just enter the value yourself when the page render so value="michael" & value="michael jackson" are optional !
Then you can retrieve those input elements like this :
#RequestMapping(value = "upload", method=RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response, #RequestParam("shortName")String shortName, #RequestParam("fullName")String fullName
#RequestPart("dataFile") MultipartFile file
){ .... }
Good Luck !
In your controller, try something like this,
#RequestMapping(value = "/your/url/{formParamenter}", method = RequestMethod.GET)
public String yourfunction(#PathVariable("formParameter") Type formParameter{}
The Type is the type of data, (String/int/float..etc).
In your case just change RequestPart to #PathVariable