How to remove default index.html mapping in spring boot? - spring

I'm using Spring boot + React JS in my application.
When user logins to the application, I'm building user details object inside my controller and puts the user details object into session attribute. Then controller tries to return ModelAndView object with views as index.html.
Sometimes index.html is rendered first by default before calling the user details method. I wants to remove this default index.html mapping behavior and always it's has to render from controller's MAV.
I tried couple of options by putting setCachable as false and must revalidated header but nothing worked out for me.
Appreciate any assistance on this.

Related

Spring security logging form inside another page

I have found really lot of examples, how to make spring security custom logging page and how to configure redirect to it if authorization is needed.
But I can’t find how to make logging form inside another page, for example inside home.html.
I can imagine I will use Thymeleaf fragments to insert custom logging page inside home page. But I don’t know how to connect it to authentication process (also without any redirection to home.html)
Thank you for replies
Petr

How to add a fileupload method in my existing mav controller to accept ajax file upload submit

My situation is: I have a Spring MVC project is running now, I need to include a file in normal form object to submit to server side to handling.
I did quite lots research recently, I found there is no way to submit a file in a regular form as file upload submit form has to configured to support file encode (enctype="multipart/form-data"). it is impossible in regular form.
Ajax provides a way to do the file uploading in javascript by javascript function call, but they need a individual servlet to listen to the javascript call. My project already did everything in modelandview controller to handle form object and model object logic.
My question is: May I add a file upload method in my modelandview controller to listen to my Ajax function call? If it is true, what's looks like?
Please help!!
After a while works on this issue, I found some thing is very interesting. If I want to use some thing else in my Spring MVC controller. I have change my controller class to "extends MultiActionController", which means make the controller to support multiAction, and then you can add other class and action in Spring MVC modelandview style controller, even you can use "webservlet" or "ResponseBody" style controller functions in your MVC controller, you don't need create other new controller to support Ajax controller, you only need update your existing controller by adding some functions. it is much easy to manage you existing code too.

MVC3 Reverse Routing

I'm working on a project converting older .aspx pages to newer MVC pages. I'm currently using the routing function to map the existing legacy pages to use the newer MVC pages behind the scenes. This is working great because it is preserving the existing URL, however, whenever the controller needs to redirect the user OR an MVC Url.Action link is used, the URL it sends the user to is the MVC url and not the routed legacy page url. I realize that this is how its suppose to be functioning out of the box but I was wondering if MVC has "reverse routing" functionality of some sort. By reverse routing I am looking for something that allows the Url.Action("action") to return the routed .aspx associated url with the specified action instead of the default /controller/action link.
So for instance,
here is a sample route
context.MapRoute("AboutUs", "pages/aboutus.aspx", new { controller = "default", action = "aboutus" });
This enables domain.com/pages/aboutus.aspx to run the new MVC action "aboutus" from the DefaultController behind the scenes perfectly.
However, if on another MVC page I have a link created by
Url.Action("aboutus")
it points them to domain.com/mvc/aboutus. I would like it to point them to domain.com/pages/aboutus.aspx to keep uniformity across the site.
Thanks for any insights that you can provide. I might end up having to override the Url.Action method to look up the route and return the mapped url but I just wanted to check for existing functionality so I don't reinvent the wheel.
Try using the RouteUrl extension method, which matches the route by route name rather than route parameters, like so:
Url.RouteUrl("aboutus") // route name
MSDN: http://msdn.microsoft.com/en-us/library/dd460347

JSP includes and MVC pattern

I am new to JSP/Servlets/MVC and am writing a JSP page (using Servlets and MVC pattern) that displays information about recipies, and want the ability for users to "comment" on it too.
So for the Servlet, on doGet(), it grabs all the required info into a Model POJO and forwards the request on to a JSP View for rendering. That is working just fine.
I'd like the "comment" part to be a separate JSP, so on the RecipeView.jsp I can use to separate these views out. So I've made that, but am now a little stuck. The form in the CommentOnRecipe.jsp posts to a CommentAction servlet that handles the recording of the comment just fine. So when I reload the Recipe page, I can see the comment I just made.
I'd like to:
Reload the page automatically after commenting (no AJAX for now)
Block the user from making more than one comment on each Recipe over a 1 day timeframe (via a Cookie). So I store a cookie indicating the product ID whenever the user makes a comment, so we can check this later? How would it work in a MVC context?
Show a message to the user that they have already commented on the Recipe when they visit one
which they have commented on
I'm confused about using beans/including JSPs etc on how to achieve this.
I know in ASP.NET land, it would be a UseControl that I would place on a page, or in ASP.NET MVC, it would be a PartialView of some sort. I'm just confused with the way this works in a JSP/Servlets/MVC context.
you can use response.sendRedirect() or forward APIs in javax.servlet to redirect to a new page or refresh the same page (redirect to the same page/path so that the beans/data gets refreshed)
about restricting to one comment per day - yes you can use cookie but the problem is that user might use another browser type (chrome, FF, Safari) and will be able to make multiple comments.
Ideally you should store the lastCommentTime in the model/persistent store and tie it to the user information - this way your model object can expose an API that checks the last comment time and returns true/false depending on whether user can comment or not.
You can use this API in your servlet/JSP to show/hide the comment button, for example and also show a message

Controller tests ignore global filters in ASP.Net MVC?

An ASP.Net MVC 3 application I've just started writing needs to have authentication for all controllers with the exception of the AccountsController. Instead of decorating all of my controllers with [Authenticate] I've written two custom attributes. The first inherits from AuthorizationAttribute and it checks to see if the current action is decorated with the other, AllowAnonymous. If it isn't it uses Forms Authentication to see if the current user is authenticated and redirects to the login page if not.
To apply this to all of the controllers I've added my custom authorization attribute to the GlobalFilterCollection in RegisterGlobalFilters in global.asax.cs.
This all works in the browser but I was surprised when I ran the default tests that come with an MVC Internet app to see that the HomeControllerTest test for the home page passed and said that the "Welcome To ASP.NET MVC" text was in the ViewBag. I would have expected this test to fail as when viewed in a browser this action would redirect to the login page unless a user had logged in.
Is my approach to authentication wrong or do I need to write something in the tests to apply the global filters? Stepping through the unit tests suggests that the filters are being added but they're not applied in the way I expect.
Your approach sounds fine. However, you will need to test controllers and action filters separately. See the following question

Resources