I am using Spring 5 for a web application. Here to check whether my search query based on some inputs from html page is returning Empty value or NOT. If empty then I am showing a no Data found notification.
I have used two methods in controller for this.
After filling the search fields I make an ajax call to the controller on button click. If data is found the controller method returns a flag which i check in the JavaScript code to check data is available or not.
If data is present then I change the attribute of form element like action to another URL and target="_blank" and make a form submit.
I have to generate a jasper report on final submit.
BUT to avoid two query hits on DATABASE , while making the first query hit on database I am storing the value to a INSTANCE VARIABLE in spring controller,
Which I am retrieving on final form submit and generating the report from the data.
I searched for answers but not getting a perfect idea how to apply in my code,
It would be of great on your part to share me with right way to implement.
******JAVA CODE*******
public class HrmsSalarySlipSummaryReportController {
List<Object[]> allowanceDetails;
List<Object[]> deductionDetails;` #PostMapping(value = "/reportIsEmptyCheckSal", headers = "Accept=application/json", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> summaryListIsEmpty(#RequestBody HrmsSalarySlipReportDTO slipReportDTO) {
allowanceDetails = summarySalReportServ.getAllowanceDetails(slipReportServ.getIntParameters(slipReportDTO, userSession.getUlbId()));
deductionDetails = summarySalReportServ.getDeductionDetails(slipReportServ.getIntParameters(slipReportDTO, userSession.getUlbId()));
return ResponseEntity.ok(summarySalReportServ.isJsonEmpty(allowanceDetails, deductionDetails));
}`
How to handle concurrency issue along with checking the empty returned result
Thanks.
Related
Two DIFFERENT post request ARE MAPPING ON the SAME page or URL. I trying to make one post request for one button do something and have the other submit button do something different
Ive tried naming the forms and an if statement in the controller using if ( action.equals("b1"))
I tried two separate controllers to handle each submit button
Java
#RequestMapping(value = "", method = RequestMethod.POST)
public String processApproveandProceedForm(#RequestParam int seoId, Model model){
SEO seoInDB = seoDao.findById(seoId);
seoInDB.setApproved(true);
seoDao.save(seoInDB);
return "redirect:/admin/seo/?seoId=" + seoInDB.getId();
}
#RequestMapping(value = "", method = RequestMethod.POST)
public String processApproveForm(#RequestParam int partnerId, Model model){
Partner partnerInDB = partnerDao.findById(partnerId);
partnerInDB.setApproved(true);
partnerDao.save(partnerInDB);
return "redirect:/admin" + partnerInDB.getId();
}
Im setting up a page where you can approve an instance of an object called SEO or Partner. Then when you click approve and proceed the SEO will take you to a page to add Students or if you approve a Partner it will redirect to the home page. All of this is displayed on the home page and I want to handle it from the controller using the name of the form which is b1 and b2.
this is a snippet from my controller and I get an error message:
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'adminController' method
public java.lang.String org.launchcode.GatewaySEC.controllers.AdminController.processApproveForm(int,org.springframework.ui.Model)
to {[/admin],methods=[POST]}: There is already 'adminController' bean method
It revealed that the program had a same method name 'adminController', guess you can change it to something else.
I have a scala template and I've passed a form instance with a Ticket instance populated with my data in order to render my page ticketForm : Form[Ticket]. However, part of my template renders a List<Object> items into a <ul> and I don't want to pass this list as a parameter on the template as I already have it as a property on the ticket object itself. I was using ticketForm.get().getItems but it runs the validation and throws an exception.
Is there any another way to do that?
Do not use get, just ticketForm('myproperty')
An example of generation radio group:
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/views/helper/inputRadioGroup.scala.html
In the case, if you want to process a list, like the #repeat helper
#helper.repeat(userForm("emails"), min = 1) { emailField =>
#helper.inputText(emailField)
}
Here is the realization of the repeat helper:
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/views/helper/Helpers.scala#L80
Here's the basic requirement:
An http request is received by page A that may have parameters defined.
If parameters are defined, page A processes the request and automatically forwards to page B. If parameters are not defined, display page A with a form for the user to fill in and submit. On submit, process the request and forward to page B.
Basically, I want to bypass the need for the user to enter data via onSubmit() if the data has already been provided as input parameters to page A. How can I do this?
Page A and B are implemented by extending the (deprecated) SimpleFormController. One way I've done this before is to place a "hidden" page (A′) that accepts the request and, if parameters are defined, processes the data and redirects to B. If the parameters are not provided, then I redirect to A where the processing is done.
This doesn't seem efficient to me, as it duplicates a lot of the processing code.
I'm not providing any code, since it doesn't easily explicate the question.
I hope I understand but you want a method in controller that will direct based on information provided.
Here is something i knocked up quickly that i hope will help solve the problem:
#RequestMapping("/pageA/{parameter}")
public String displayPageA(Model model, #PathVariable String parameter) {
if(parameter == null)
{
model.addAttribute("your_form", new YourForm());
return "pageA";
}
else {
return "redirect:/pageB/"+parameter; // this is if you want the parameter passed on
}
}
I am want to create simple form for searching records via one parameter (for example, name).
Seems like creating a class with one property (name) and than use helpers for forms - is not a best way.
Is there any examles how can I get POST data from request and fetch property value from that data?
Thanks a lot for wasting your time.
You already answered your own question, I just want to provide some more information:
You are right about creating a class with one single property, however keep in mind that you can use validation annotations (like #Required, #Email, etc.) in this class - so if there is some (super) complex logic behind this property this might also be a valuable option.
The second solution would be to use DynamicForms - you use them when you don't really have a model that is backing up the submission form. It goes like this:
public static Result index() {
DynamicForm requestData = Form.form().bindFromRequest();
String name = requestData.get("name");
return ok(name);
}
And of course the third option to get the values is like you mentioned:
String name = request().body().asFormUrlEncoded().get("name")[0];
If you do not what to use form validation, I don't think you need to create a class. Instead, you can use AJAX function like $.ajax(), that will be route to your specific controller function. Moreover, you can call your model function from your controller then at last return the result. The result will be caught by the $.ajax() function.
$.ajax
type: "POST"
url: url
data: data
success: success
dataType: dataType
I am creating a wizard-like interface consisting of 3 jsp pages and 3 Struts actions using Struts 1.3. The flow is like below:
page1>action1 ->page2>action2 -> page3>action3
I use a session form bean (an action form with session scope) to share data between requests. The problem I am having is that the data I submitted in page2 is available in action 2, but not in action 3. I am in doubt it might be I don't have a form on page3 to hold those data, or because I call action3 via jQuery post method instead of a regular form submit, but I am really not sure.
I have been digging all the internet for almost a day and still no luck. Could anyone offer some help. Thanks a lot.
The reset() method on the form is being called with each request and thus you are losing state. You can programmatically control this.
public class MyForm extends ActionForm {
boolean reset = true;
private String[] checkboxes = {};
#Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
if (reset) {
this.checkboxes = new String[];
// etc
}
reset = true;
}
public void doNotReset() {
reset = false;
}
}
Have action2 call doNotReset() on the form.
I suppose that you might have assigned a same form to both the action in StrutsConfig.xml and hence it is not giving the ClassCastException. By the way, if you want to access the same form bean which was filled on action 2 stuff, do the following
Look at the strutsConfig file for actionMapping of both the actions (2 and 3). keep the name of form different for separate action (e.g. form2 for action2 and form3 for action3).
In Action3, instead of casting the form, use this form2 = (FormBean2) session.getAttribute("form2");
The reason for above is since both the actions are using the same form, struts might have overwriting it. Hopefully above will solve your problem.
Thank you for all your inputs. Here is how I solved my problem. I don't really like this solution, but it possibly the neatest one I can find.
In page 3 I added hidden fields for what ever property I want to be available in action 3. Struts will store the values in those hidden field and when the form is submitted again, the data will then re-populated to the action form.
It seems to me that Struts works like this: when it loads page 3, it try to populate the form in page 3 with values of myForm. When the form is submitted, the process is reversed, it populate myForm with values from the user's form. The problem is that, before populating myForm with values submitted by user, it resets myForm's properties. And because after reseting, it doesn't find the value for those fields, it leaves it empty.
I don't think it makes sense for Struts to work that way, but... so be it.
How are you accessing the form bean of page2 in action2 as well as in action3.
I suppose you are accessing the wrong way. Are you getting an exception regarding invalidCast or something.