Spring MVC 4 display an error message [duplicate] - java

My objective is to pass model attributes from controller to JSP page during a redirect and avoid the attribute being displayed in URL. The source code below is validating login from datastore using java data objects.
Controller:
#Controller
public class LoginController {
int count;
PersistenceManager pm = PMF.get().getPersistenceManager();
//Instance of data class
User user;
ModelAndView modelAndView=new ModelAndView();
#RequestMapping(value="/Login",method = RequestMethod.POST)
public ModelAndView loginValidate(HttpServletRequest req){
//Getting login values
String uname=req.getParameter("nameLogin");
String pswd1=req.getParameter("pswdLogin");
count=0;
user=new User();
//Generating Query
Query q = pm.newQuery(User.class);
q.setFilter("userName == userNameParam");
q.declareParameters("String userNameParam");
try{
List<User> results = (List<User>) q.execute(uname);
for (User u: results) {
String userName=u.getUserName();
if(userName.equals(uname)){
System.out.println(u.getPassword());
if(u.getPassword().equals(pswd1)){
count=count+1;
modelAndView.setViewName("redirect:welcome");
modelAndView.addObject("USERNAME",uname);
return modelAndView;
}
//rest of the logic
}
JSP:
<h1>Welcome ${USERNAME} </h1>
My current URL is /welcome?USERNAME=robin
My goal is to display it as /welcome
Also, my page is supposed to display "Welcome robin" whereas it displays only Welcome.

RedirectAttributes only work with RedirectView, please follow the same
#RequestMapping(value="/Login",method = RequestMethod.POST)
public RedirectView loginValidate(HttpServletRequest req, RedirectAttributes redir){
...
redirectView= new RedirectView("/foo",true);
redir.addFlashAttribute("USERNAME",uname);
return redirectView;
}
Those flash attributes are passed via the session (and are destroyed immediately after being used - see Spring Reference Manual for details). This has two interests :
they are not visible in URL
you are not restricted to String, but may pass arbitrary objects.

You need to be careful here because I think what are you trying to do is not supported for a good reason. The "redirect" directive will issue a GET request to your controller. The GET request should only retrieve existing state using request parameters, this is the method contract. That GET request should not rely on a previous interaction or on any object stored some where in the session as a result of it. GET request is designed to retrieve existing (persisted) state. Your original (POST) request should have persisted everything you need for you GET request to retrieve a state.
RedirectAttributes are not designed to support you in this case, and even if you managed to correctly use it it will only work once and then they will be destroyed. If you then refresh the browser you will get an application error because it cannot find your attributes anymore.

Related

Java/Spring make new ModelAndView that refers to previous page / page where get request was made

I have some trouble forwarding after a request to a previous url.
I've got a team-page, (.../team/teamMembers/{id}) in which all the team players from a team are listed. There is a delete button, to delete a team player from the team. After this request has been made, i want to return to the team/teamMembers/{id} page again, which has now been edited.
My request looks like this:
#RequestMapping(value = "/deleteTeam/{id}", method = RequestMethod.GET)
public ModelAndView deleteTeam(#PathVariable Integer id) {
ModelAndView modelAndView = new ModelAndView("/team/teamMembers/{id}");
memberService.deleteTeam(id);
List<Member> members = memberService.getMembers();
modelAndView.addObject("members", members);
String message = "Member was successfully deleted from team.";
modelAndView.addObject("message", message);
return modelAndView;
}
But of course, team/teamMembers/{id} does not work, since {id} doesn't have a value, and is not a good url string. Furthermore, the id from the deleteTeam/{id} is a memberID and not a teamID.
How can I redirect to the page from which the getRequest has been made ?
You need to teamId also while deleting a member from team, because you will redirect again teamMembers page.
// Also get teamId while deleting via path variable or as a parameter in controller
// /deleteTeam/{teamId}/{memberId} may be used
// or more user friendly readable path /team/{teamId}/delete/member/{memberId}
return new ModelAndView("redirect:/team/teamMembers/"+ teamId);
So that method will work as you wish.

Why ajax parsing will not allow Spring MVC controller to redirect jsp pages?

I have been using ModelAndView as the object type in my controller to handle method to do data binding with front-end html form.Such as this:
#RequestMapping(value = "/postSth", method = RequestMethod.POST)
public ModelAndView postSomething(#RequestParam("name") String name){
ModelAndView model = new ModelAndView("displayPage");
model.addObject("name", name);
return model;
}
This method will allow me to bind the name and I can display the name to the displayPage.jsp after the POST method. displayPage is the JSP page name and by using the InternalResourceViewResolver from Spring framework.
Lately I have been using jQuery ajax function to pass the data to my controller, and I am working on method looks like this:
#RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
#ResponseStatus(value = HttpStatus.OK)
public ModelAndView postSomething(#RequestBody String name){
ModelAndView model = new ModelAndView("displayPage");
model.addObject("name", name);
return model;
}
I noticed I can successfully grabbing the JSON string with this controller, but this method will not redirect my webpage to the displayPage and the data binding with .addObject no longer work.
Why it does not work? How do I change it to still allow me to direct to the displayPage.jsp? I understand I can do the redirect with javascript at front-end, but it is not what I want to do.
I understand your requirement as you trigger an ajax call but instead of loading the output of data to current jsp you need to populate it in new jsp.
I have had the similar requirement, I used jquery load() to implement it. It worked fine in my case. Basically its like a 3 step process;
Trigger Ajax call to the controller to get the required data to be loaded on new jsp.
use load() once required data is available to load the new jsp in current page.(you can use some div to load it, if you want to completely replace current page then empty the contents of current page and laod with new jsp)
Write javascript/jquery codes to manipulate the dom in new JSP which we rendered on previous step.
Below is the snippet of how it can be done.
#RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
#ResponseStatus(value = HttpStatus.OK)
public String postSomething(#RequestBody String name){
return model;
}
<div id='currentContent'>
<!--Existing content -->
</div>
var ajaxResult = '';
$.ajax({
type : POST,
url: "postSth",
async:false}).then(function(data){
//DIV ID here would be the ID which you need to have in your page to append the HTML content.
//if you want to completely reload the page then use the id where your current contents are displayed
//Inside your somenewjsp.jsp you can write your jquery codes to access the variable ajaxResult on document ready
ajaxResult = data;
$('#currentContent').load('/somenewjsp.jsp');
});
});
Please share your results on this approach.

Spring MVC forwarding to controller with different HTTP method

I have login controller methods like so:
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
// do stuff with locale and model
// return an html page with a login form
return "home";
}
#RequestMapping(value = "/account/login", method = RequestMethod.POST)
public String login(Model model, /* username + password params */){
try {
// try to login
// redirect to account profile page
return "redirect:/account/profile";
} catch (LoginException e) {
// log
// here I want to reload the page I was on but not with a url /account/login
// possibly using a forward
model.addAttribute("error", e.getMessage());
return "forward:/home";
}
}
The above code works on successful log-in attempt. However, it fails when the log-in attempt fails because Spring's forward uses the current request with the same HTTP method. So because I used a POST to send my username/password (which caused log-in to fail), the forward will also use POST to go to the handler method for /home, home(), which is expecting a GET.
Is there any way in Spring to redirect to another controller method with a different HTTP method while maintaining the current model (since I want to show the error message)?
This is on Spring 3.2.1.
Do a redirect instead:
return "redirect:/home";
If you need Model attributes to be available after the redirect, you can use flash attributes.

Persisting object data between controllers in Spring

I have an object called Request which is the main object of my portal that stores all the information of a request, the user, their form selections, etc. How to I persist all the previous information in between the different forms? In each .GET I have to set the request object, and then in each .POST, the only information that is passed to it is what is in the forms on the .GET pages. So on each page I have to have hidden fields such as
<form:input path='requestId' style='display:none' />
<form:input path='currentUserId' style='display:none' />
<form:input path="step" style='display:none' />
I need these fields, and would also like to have the rest of the fields in the request object that are not on the form without having to repeat that for each and every field in my object.
#RequestMapping(value = "/review", method = RequestMethod.GET)
public ModelAndView showCorReview(#RequestParam(value = "requestId") String requestId,
#CookieValue(value = "EMP_ID", defaultValue = "168") int userId)
{
Request request = requestManager.getRequestById(Integer.parseInt(requestId));
request.setCurrentUserId(userId);
String pageTitle = "2.1: Initiate New Service Request -- (Review)";
ModelAndView mav = new ModelAndView();
mav.setViewName("newRequest/review");
mav.addObject("title", pageTitle);
mav.addObject("request", request);
mav.addObject("cpm", userManager.getUserById(request.getCpm()).getName());
return mav;
}
#RequestMapping(value = "/review", method = RequestMethod.POST)
public String saveReview(Request request, #RequestParam(value = "commentData", required = false) String[] additionalComments)
{
if (additionalComments != null)
commentLogManager.addCommentLog(additionalComments, request);
if (request.getRejectReason() == "")
{
request.setCpm(admin.getCPM(request.getContract()).getId());
request.setCor(admin.getCOR(request.getContract()).getId());
requestManager.updateRequest(request);
}
else
{
if (request.getSubmitType().equals("return"))
{
request.setNextStep(1);
requestManager.moveRequestToStep(request);
}
}
return worksheetUrl + request.getId();
}
Alternatately I could also in the .POST do the
Request request = requestManager.getRequestById(Integer.parseInt(requestId))
Then use setters on all the form fields, but again, I would prefer the data to actually persist on it's own without explicitly calling that.
#Tim, if I understood your requirement correctly, you have a sequence of forms and you would like to transfer information from one form to the next without having to hit a database or copy over request variables from one form to another. I could support #JB Nizel's suggestion of employing HTTP Session, but you may not want to make the session "heavy"; after all, it is the next most persistent scope after application-scope.
Spring Web Flow may be the answer. Flow-scope will allow you to build up form-state as the user progresses from one form to the next. Plus you don't have to worry about form-scoped variables when the flow finishes, unlike session variables that you don't want to have lingering around.

When the validator finds form errors, the form page is redisplayed at the POST url

An item is displayed at this URL:
/item/10101
using this Controller method:
#RequestMapping(value = "/item/{itemId}", method = RequestMethod.GET)
public final String item(HttpServletRequest request, ModelMap model,
#PathVariable long itemId)
{
model = this.fillModel(itemId);
return "item";
}
The page contains a form that submits to the following method in the same controller:
#RequestMapping(value = "/process_form", method = RequestMethod.POST)
public final String processForm(HttpServletRequest request,
#ModelAttribute("foo") FooModel fooModel,
BindingResult bindResult,
ModelMap model)
{
FooModelValidator validator = new FooModelValidator();
validator.validate(FooModel, bindResult);
if (bindResult.hasErrors())
{
model = this.fillModel(fooModel.getItemId());
return "item";
}
return "account";
}
If the validator finds errors in the form, it redisplays the item but instead of displaying it at the original url:
/item/10101
it displays it at its own url:
/process_form
Is it possible to redisplay the form at the original URL?
/item/10101
(I've tried getting the referrer and redirecting to it in processForm but then all of the model contents end up displayed as URL name/value pairs:)
#RequestMapping(value = "/process_form", method = RequestMethod.POST)
public final String processForm(HttpServletRequest request,
#ModelAttribute("foo") FooModel fooModel,
BindingResult bindResult,
ModelMap model)
{
String referrer = request.getHeader("referer");
FooModelValidator validator = new FooModelValidator();
validator.validate(FooModel, bindResult);
if (bindResult.hasErrors())
{
model = this.fillModel(fooModel.getItemId());
return "redirect:" + referrer;
}
return "account";
}
Short answer: No.
What happens is a server-side redirect (forward), which is within the same request, and so the submitted values are preserved (and displayed in the form)
The url will change if you use a client-side redirect (return "redirect:item";), but in that case a new request will come and the submitted values will be lost.
But here are two options that you have:
use the same URL in the mappings for both methods and distinguish them based on request method - GET for the former, POST for the latter. This might be confusing, so document it.
find / implement flash scope for spring-mvc. There's nothing built-in. The flash scope means that values are preserved (in the session usually) for a submit and the subsequent redirect. This option includes the manual handling, by putting the submitted object in the session, and later retrieving & removing it

Categories

Resources