Passing messages from AuthenticationFailureHandler and LogoutHandler to JSP - spring

I my application I have created custom AuthenticationFailureHandler and LogoutSuccess handler classes.
Now I want to pass messages from these classes to JSP which should be accessed using struts2 jstl tags.
I dont want to use url parameters for passing messages to jsp page.(login.action?msg=abc)
Any ideas on this?

One of the way to do it, is to put your message to session. In JSP you can access session like that:
<s:property value="#session.message"/>

Related

Is there a way to retrieve data from redis in my jsp pages?

I am creating a spring boot server, and I would like the initial page (index.jsp) to be a datatable with information coming from the redis database. Anyone know how to do this with javascript or inside jsp pages?
First of all JSPs are outdated technology so I recommend you to go for thymeleaf which is a perfect choice with spring boot instead of JSP.
JAVA approach :
Just create a mapping with / base url and fetch Redis data using normal approach and use it in UI.
GetMapping("/")
public String index(){
//Fetch Redis data here add into model attributes
return "index";
}
JavaScript/inside JSP approach :
You can create a mapping method with some url like above and make an AJAX call on page load from the JSP page and get the JSON format data and use it.
Don't forget to put #ResponseBody annotation on top of the method which will return the data(JSON) instead of the page.
Refer call ajax to spring controller

For validating session attribute, which is better in spring - Interceptor or Spring AOP?

In my application, after a user is logged in, every time he sends a request (get/post), before calling the method in controller, i want to verify the session attribute set in the request (i set a session attribute during his login). I see that this can be implemented through spring interceptors (OR) spring AOP. which one should i use?. I have a feeling interceptors are outdated. Or is there a way in spring security which does this for me?
So you want this intercept to happen only for all the controller methods ..? Does the controller have Base URL that its getting invoked for (post/get/delete)...? Is it more like you want to intercept the http request for a particualt URL ..? like this one
<intercept-url pattern="/styles/**" filters=" .." />
If your use case is boiled down to a particular URL pattern then you can write a custom filter extending GenericFilterBean and you can plug it to the filters attribute.So this will get called for every request matching url pattern and in your custom filter you can do whatever you wanted to do.
What if you try implementing a simple Filter? You can extend already existing Spring filter, or create your own by implementing javax.servlet.Filter
The spring security way seems the best way to me with access to specific roles also can be assigned. very good example given in http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

Spring validation in multiple forms

Can we display error messages in one jsp but in 2 different forms.I am validating my bean using spring validation.Its working fine but if there is an error in the binding results then I am able to display the error message in the form from where action is executed but not in the other resultant form.
is the error tag in spring validation is mapped to jsp form from where that action is called.
Your BindingResults is just an object, which you can put in your Model and use on the page however you want. You may need to put it into Session if you plan to use it on a separate form.

Spring MVC 3.0 - restrict what gets routed through the dispatcher servlet

I want to use Spring MVC 3.0 to build interfaces for AJAX transactions. I want the results to be returned as JSON, but I don't necessarily want the web pages to be built with JSP. I only want requests to the controllers to be intercepted/routed through the DispatcherServlet and the rest of the project to continue to function like a regular Java webapp without Spring integration.
My thought was to define the servlet-mapping url pattern in web.xml as being something like "/controller/*", then have the class level #RequestMapping in my controller to be something like #RequestMapping("/controller/colors"), and finally at the method level, have #RequestMapping(value = "/controller/colors/{name}", method = RequestMethod.GET).
Only problem is, I'm not sure if I need to keep adding "/controller" in all of the RequestMappings and no matter what combo I try, I keep getting 404 requested resource not available errors.
The ultimate goal here is for me to be able to type in a web browser "http://localhost:8080/myproject/controller/colors/red" and get back the RGB value as a JSON string.
You are not correct about needing to add the entire path everywhere, the paths are cumulative-
If you have a servlet mapping of /controller/* for the Spring's DispatcherServlet, then any call to /controller/* will be handled now by the DispatcherServlet, you just have to take care of rest of the path info in your #RequestMapping, so your controller can be
#Controller
#RequestMapping("/colors")
public class MyController{
#RequestMapping("/{name}
public String myMappedMethod(#PathVariable("name") String name, ..){
}
}
So now, this method will be handled by the call to /controller/colors/blue etc.
I don't necessarily want the web pages to be built with JSP
Spring MVC offers many view template integration options, from passthrough to raw html to rich templating engines like Velocity and Freemarker. Perhaps one of those options will fit what you're looking for.

How to use annotated Spring controllers with Freemarker to return custom ajax respones

I primarily use annotated controllers that return ModelAndView object used by Spring and Freemarker to render and return pages to browsers - works great. I'd now like to respond to ajax gets and posts that return html fragments as payload inside an XML message.
The problem I'm having not returning a ModelAndView objects seems to confuse Spring WRT to #ModelAttribute (s) and session objects that support data to / and from forms.
I've had to hack the freemarker template to support i18n messages, etc. I'm finding that simply rendering an FTL file while also trying to utilize Spring's object rendering from froms is becoming quite a rabbit hole.
I'm trying to manually (guess what and) do the things that get done behind the scenes and it's not working optimally. To edit an object I'm manually placing the object in the session on rendering the edit form. When the post comes back there are residual/different values in the object I get back out of the session AND the ModelAttribute that Spring rendered for me.
Bottom line? Questions about Spring, Freemarker and custom non-ModelAndView responses that are rendered by Freemarker.
Add another instance of FreeMarkerViewResolver that only that has viewNames ='*.frg'. The viewClass should be your own class that renders the template the way you want by overriding the processTemplate method.

Resources