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.
Related
I'm trying to develop an application in spring boot + thymeleaf, and I'm able to retrieve the logged in user details in the profile tab from the MySQL database, but when I try to change one or two field details (update) and hit the update button it is showing me an error message - Fri Sep 04 20:39:47 IST 2020
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
see my controller code (I'm using #RestController annotated on top of the class)-
#RequestMapping(value = "/profile", method = RequestMethod.PUT)
public ModelAndView updateProfile(#ModelAttribute Customer customer, HttpSession session) {
ModelAndView model = new ModelAndView();
Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
if(exist != null) {
if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
cRepo.save(customer);
model.addObject("msg", "User Details has been successfully updated!!");
model.setViewName("profile");
}
}else {
model.addObject("exist", "Please enter correct email address!");
String email = (String) session.getAttribute("emailsession");
Customer cust = cRepo.findByCustEmail(email);
model.addObject("customer", cust);
model.setViewName("profile");
}
return model;
}
Thymleaf code (html) -
<div align="center" class="alert alert-success" th:if="${msg}" th:utext="${msg}"></div>
<div align="center" class="alert alert-danger" th:if="${exist}" th:utext="${exist}"></div>
<!-- Modal HTML -->
<div id="myModal">
<div class="modal-dialog modal-login">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Profile Details</h4>
</div>
<div class="modal-body">
<form name="myForm" th:action="#{/profile}" th:object="${customer}" method="post">
<div class="form-group">
<i class="fa fa-id-card"></i>
<input name="id" type="text" class="form-control" placeholder="Enter Id" th:field="${customer.custId}" disabled="true" required="required" />
</div>
<div class="form-group">
<i class="fa fa-user"></i>
<input name="name" type="text" class="form-control" placeholder="Enter Name" th:field="${customer.custName}" required="required" />
</div>
<div class="form-group">
<i class="fa fa-envelope"></i>
<input name="email" type="email" class="form-control" placeholder="Enter Email" th:field="${customer.custEmail}" required="required" />
</div>
<div class="form-group">
<i class="fa fa-lock"></i>
<input name="password" type="text" class="form-control" placeholder="Enter Password" th:field="${customer.custPassword}" required="required" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-block btn-lg" value="Update" />
</div>
</form>
</div>
</div>
</div>
</div>
I want when user login and visit he/she should be able to check his/her profile(which I'm able to do working code) and when the user wants to update few fields(1-2 based on choice) and hit update he/she should be able to update the details (not create new user or record) because when I use #Controller on top of class then this code work and create new user instead update.
Your controller is annotated with #RequestMapping(value = "/profile", method = RequestMethod.PUT) which makes it a PUT endpoint. However, your request is clearly a POST. If we look at your html form it contains method="post". HTML forms only support GET and POST as valid methods so you need to update your endpoint to be a POST endpoint.
tldr;
RequestMapping(value = "/profile", method = RequestMethod.PUT)
to
RequestMapping(value = "/profile", method = RequestMethod.POST)
You request mapping in is POST but Controller has set to accept request as PUT.
<form name="myForm" th:action="#{/profile}" th:object="${customer}" **method="post"**>
#RequestMapping(value = "/profile", method = **RequestMethod.PUT**)
Just keep these in similar way both should be same.
Please check what I find and resolve this.
#RequestMapping(value = "/profile" ,method = RequestMethod.POST)
public ModelAndView updateProfile(#ModelAttribute Customer customer, HttpSession session) {
ModelAndView model = new ModelAndView();
Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
if(exist != null) {
if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
**exist.setCustId(exist.getCustId());
exist.setCustName(customer.getCustName());
exist.setCustEmail(customer.getCustEmail());
exist.setCustPassword(customer.getCustPassword());**
cRepo.save(exist);
model.addObject("msg", "User Details has been successfully updated!!");
model.addObject("customer", exist);
model.setViewName("profile");
}
}else {
model.addObject("exist", "Please enter correct email address!");
String email = (String) session.getAttribute("emailsession");
Customer cust = cRepo.findByCustEmail(email);
model.addObject("customer", cust);
model.setViewName("profile");
}
return model;
}
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.
Im working on spring MVC webApp project and I'm sort of stuck at this one step. I have a JSP page that have buttons with value of $1.00 .25 etc. when user click the buttons the value need to be stored for purchase. i have a method in the controller but unsure what to pass in the parameter for the input. Please point me to the right the direction. thanks.
<div class="form-group" >
<input class="form-control" style="text-align: center" id="display-money" placeholder="Enter Money" required/> <!--to diplay total -->
</div>
<div class="col-md-6">
<button method="POST"
action="addOne-btn"
value="addOne"
modelAttribute="add-one"
type="button"
name="one"
id="addOne"
class="btn btn-default ">
Add Dollar
</button>
</div>
<div class="col-md-6">
<div class="form-group">
<button method="POST"
value="addQrtr"
action="addQrtr-btn"
type="button"
name="qrtr"
id="addQRTR"
class="btn btn-default">
Add Quarter
</div>
</div>
</div>
-------------------------------------------------------------------
//controller
#RequestMapping(value = "/money", method = RequestMethod.GET)
public String money(HttpServletRequest request, Model model){
BigDecimal moneyIn = new BigDecimal(0);
String input = request.getParameter();//pass in onlcick from jsp.
if(input.equals("addOne")){
moneyIn.add(new BigDecimal(1.00));
}
if(input.equals("")){
moneyIn.add(new BigDecimal(.25));
}
}
Use #ModelAttribute in your submit controller.
#RequestMapping(value = "/money", method = RequestMethod.GET)
public String money(HttpServletRequest request, #ModelAttribute Bean bean){
BigDecimal moneyIn = new BigDecimal(0);
String input = bean.get***();//pass in onlcick from jsp.
if(input.equals("addOne")){
moneyIn.add(new BigDecimal(1.00));
}
if(input.equals("")){
moneyIn.add(new BigDecimal(.25));
}
}
I'm attempting to utilize a REST API which is created using Spring by using AngularJS controllers. All I want to do, is send the credentials from the form using AngularJS to the Spring controllers in the back end; however, they don't seem to be communicating at all. Whenever I push the login button, the page simply refreshes. I placed loggers in the Spring controller I'm attempting to call, but they never print anything out which means the controller is never being called.
AngularJS controller:
angular.module('landing', [ 'ngRoute' ])
.controller('authentication',
function($rootScope, $scope, $http, $location) {
$scope.credentials = {};
$scope.login = function() {
$http.post('http://localhost:8090/login', $.param($scope.credentials), {
headers : {
"content-type" : "application/json"
}
}
};
});
The Spring controller I'm attempting to utilize:
#RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<BusinessUser> login(#RequestBody BusinessUser inputUser) {
logger.info(inputUser.getUsername());
logger.info(inputUser.getPassword());
BusinessUser requestedUser = businessUserService.findByUsername(inputUser.getUsername());
if(requestedUser != null)
if(BCrypt.checkpw(inputUser.getPassword(), requestedUser.getPassword()))
return new ResponseEntity<>(requestedUser, HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Lastly, here's the form:
<body data-spy="scroll" data-target=".navbar" ng-app="landing">
<div id="home" class="border-shadow">
<div class="container-fluid container-background-color">
<div class="row border-shadow">
<div class="col-lg-7">
<h2 class="heading-margin main-color"><img class="imagesize" src="images/icon.png"/><b>Test Company</b></h2>
</div>
<div class="col-lg-5" ng-controller="authentication">
<form role="form" ng-submit="login()">
<div class="form-group row row-margin">
<div class="col-lg-5">
<input type="text" class="form-control" ng-model="credentials.username" placeholder="Username" required>
</div>
<div class="col-lg-5">
<input type="password" class="form-control" ng-model="credentials.password" placeholder="Password" required>
Forgot Your Password
</div>
<div class="col-lg-2">
<button type="submit" class="btn btn-success">Log in</button>
</div>
</div>
</form>
</div>
</div>
</div>
I can send requests using a Chrome add on called "Postman" and the results come back perfectly, so it's an AngularJS problem. Any idea what's wrong?
EDIT This is a Spring Boot project by the way.
I am building an application which has same functionalities both from AWS and Google Cloud. For example Creating an instance , Launching instance from Machine ID, Creating snapshot, Liting all Instances,etc. Which I called using REST calls.
For example:
<form method="post" action="rest/create/new" class="form-inline">
<label for="user">User Name</label> <input type="text" id="user"
name="user" class="form-control" size="50"
placeholder="Enter Username">
<button type="submit" class="btn btn-info">Start New Machine</button>
</form>
<form method="post" action="rest/launch/start" class="form-inline">
<label for="AMId">Launch Existing Machine</label><br> <input
type="text" id="AMId" name="AMId" class="form-control" size="50"
placeholder="Enter Instance ID">
<button type="submit" class="btn btn-info">Launch</button>
<br>
</form>
<br> <br>
<form method="get" action="rest/create/listAll" class="form-inline">
<label>Show All EC2 Instances</label><br>
<button type="submit" class="btn btn-info btn-lg">Show All</button>
</form>
<br> <br>
<form method="post" **action="rest/create/listRunning"**>
<label>Show All Running EC2 Instances</label><br>
<button type="submit" class="btn btn-info">Show All Running</button>
</form>
<br> <br>
<form method="post" action="rest/create/terminate" class="form-inline">
<label for="terminateID">Enter Instance ID</label><br> <input
type="text" id="terminateID" name="terminateID" class="form-control"
size="50" placeholder="Enter Machine ID">
<button type="submit" class="btn btn-info">Terminate</button>
</form>
<br> <br>
And I am catching these rest calls in classes.
For example,
#GET
#Path("/listAll")
public Response getAllAvailableImages(#Context ServletContext context)
Now what I want is how can I use both AWS and google cloud features, using these same calls or some other method,depending on requirement or choice?
How about:
GET /{provider}/images
POST /{provider}/images/{imageID}/start
where the variables in braces are placeholders for path parameters:
{provider} may resolve to AWS, Google or another provider
{imageID} references a unique image ID
Examples:
GET /AWS/images (gets all AWS images)
POST /GoogleCloud/images (creates new Google Cloud image)
POST /OpenStack/images/gfhdh45ff4/terminate (terminates a specific image)
If you are using Spring MVC for REST, the controller could look like:
#RestController
public class ImageController {
#Autowired
private ImageService imageService;
#RequestMapping(value = "/{provider}", method = RequestMethod.GET)
#ResponseStatus(HttpStatus.OK)
public List<Image> getImages(#PathVariable String provider) {
return imageService.getImagesByProvider(provider);
}
#RequestMapping(value = "/{provider}", method = RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
public Image createNewImage(#PathVariable String provider, #RequestBody Image image) {
return imageService.createImageForProvider(provider, image);
}
#RequestMapping(value = "/{provider}/images/{imageId}/start", method = RequestMethod.PUT)
#ResponseStatus(HttpStatus.NO_CONTENT)
public void startImageAtProvider(#PathVariable String provider, #PathVariable String imageId) {
return imageService.startImageAtProvider(provider, imageId);
}
}
The HTTP method for starting the image could be POST - and should be if starting an image is not idempotent. But I am assuming hat attempting to start an image which is already running will just be ignored.
Extra edit:
If the image IDs are unique across ALL providers, you could shorten the REST URLs regarding images:
POST /images/gfhdh45ff4/terminate (terminates a specific image)