Is there a way to change what should be home page for spring batch admin webapp?
I tried various solutions and nothing helps. What bothers me is that RequestMapping(value = { "/", "/home" }) is bidden to HomeController inside spring-batch-resources project, and I can't override that mapping.
I didn't know what would be necessary, I first tried redefining index.jsp page to farward to "/myHomePage" instead of "/home" that is by default. No result.
I tried defining my own controller to have same RequestMapping(value = "/"). No result. Ambiguous mappings exception.
I tried extending and overriding method from HomeController in my own controller, also without result, also ambiguous mappings exception.
I even added
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
NO RESULT!
I don't care much about "/home" mapping I just want to override "/" mapping, or to forward it to my page, or something like that. Is there a way?
P.S. I can't change HomeController implementation, since it is spring-batch-resources dependency for spring-batch-admin-sample...
Explanation for the answer I've given in comments, requested by #kap
It's been a long time since I worked on this, and I don't have the code available now, but as I can remember:
In one of the definitions for the application-context in XML files in resources (sorry I forgot which one) you need to add something like
<mvc:interceptor>
<mvc:mapping path="/"/>
<bean class="blabla.MyInterceptor" />
</mvc:interceptor>
and then create class MyInterceptor that implements HandlerInterceptor which will redirect the request. There are 3 methods you can use to override the behavior
preHandle
postHandle
afterCompletion
and I think that I used postHandle to implement code for redirect to my custom homepage. You can google for tutorials on redirection of requests using Spring interceptors to see how to implement that.
this worked for me:
adding this to web.xml
<servlet-mapping>
<servlet-name>Batch Servlet</servlet-name>
<url-pattern>/batch/*</url-pattern>
</servlet-mapping>
and adding this part on my own index page
<body>
<div class="pageHeader" id="pageHeader" style="height: 100px;"></div>
<p>Batch job admin</p>
</body>
Related
I am back with working in Springs. I used to work in Springs but blindly, didn't understand much. I used to get a lot of errors, very basic ones, and I am getting them again.
My problem is that, I don't know how the configuration of the Spring-MVC work.
What happens when I run the project from my STS?
I am working on the spring template project in STS.
I am getting this when I run the project.
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/common/] in DispatcherServlet with name 'appServlet'
I am totally fed up and broken.
Just 2 months of break from work, I am back at the starting block.
I don't want to post my code and make the question specific.
I want an answer that explains the way in which the server executes a spring project. Right from the running of an application(basic hello world application) to the display of the home page.
This will be helpful for all the beginners.
I tried searching for such an explanation in the net but I didn't get any proper explanation, but got a lot of basic samples. Those samples are easy to understand but are not explaining the way in which the server goes about.
Note: I am looking for an answer that explains the Springs concept. From the running of an application to the display of a home page. What all happens in this process? Where does the server start with? How does it go about?
Here is the flow initially servlet container loads the web.xml file.In web.xml we will specify that all the requests are handled by the spring FrontController that is DispatcherServlet.
We include it by adding the following code
<servlet>
<servlet-name>dispatcher</servlet-name>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Here it indicate if the url request is of *.htm it is handled by dispatcherServlet then dispatcherServlet load dispatcher-servlet.xml . Where we need to mention the mapping to controller by writing the specific url request such as
<bean name="/insert.htm" class="com.controller.MyController"></bean>
So in bean we mention that for request of /insert.htm it tells the servlet to look in the mentioned class.You need use the Annotation of #RequestMapping above the method for ex
#RequestMapping("/insert.htm")
public ModelAndView insert(HttpServletRequest req,Student student)
{
String name=req.getParameter("name");
int id=Integer.parseInt(req.getParameter("id"));
student.setId(id);
return new ModelAndView("display","Student",student);//It returns a view named display with modelclass name as `Student` and model object student
}
So when a Request url of /insert.htm appears it executes the above method it returns a ModelAndView object nothing but an view.It again goes to dispatcher-servlet.xml and looks for view Resolver the normal code that is to be added is
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
So from this it gets the logical view name and appends the prefix and suffix to it .Finally it displays the content in the view.so it looks for display in view resolver prefixes and suffixes the things and finally returns /WEB-INF/jsp/display.jsp .Which displays the jsp content
You are mapping your Spring servlet only for requests that end with .htm. The request for the root of your application does not end with .htm and so, it does not get picked up by Spring. Edit your web.xml as follows, in order to use Spring for all requests:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then, use this as the controller:
package com.mkyong.common;
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
The controller intercepts the requests for the context root of the application, adds the msg attribute to the model and redirects to the index view.
So, you need to add the index.jsp file in the /WEB-INF/views/ directory. Inside your jsp, you will be able to use the value of the msg attribute.
From what every you have posted you do no have a request mapping for the url /common/.
You will have to create another request mapping function like the one below in your controller class and create a view file also.
#RequestMapping(value = "/common/", method = RequestMethod.GET)
public ModelAndView common(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView model = new ModelAndView("common");
model.addObject("msg", "hello world");
return model;
}
Have searched around and have not found a conclusive answer to this.
I have trying to route all http requests through my dispatcher servlet, and then onto a specific controller. Ultimately I want to be able to handle resource, AJAX and a.n.other request through the central point.
I currently have the url mapping /* in place to do this. My controllers use #RequestMapping("/[My resource].*") to capture my .htm requests. Unfortunately Spring appears to use RequestDispactcher.forward to resolve the .jsp from the InternalResourceViewResolver which is then hitting the front controller again and ultimately causing a 404 error.
My question is, am I able to setup a generic catch all that will handle any HTTP request other than the regular view request ?
The HTTP handler must be able to pass requests on to other servers and resolve internal and external resources e.g. images, css etc.
Regards,
Andy
Regards
A think a better idea is to change the servlet-mapping of DispatcherServlet to / instead of /*, this is because /* makes all request come to this servlet, instead like you have found for the jsp forwards also, inspite of the fact that there is a JSPServlet mapping for the jsps, the / mapping on the other hand will be defaulted to only if a specific mapping is not found for the requested path.
Keep the app servlet mapping to / in web.xml. Like shown below.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To resolve other resources add following tag in your dispatcher servlet xml.
Here resources is the folder containing js, css, images. It is stored under Webcontent folder in maven web application structure. Change it according to your project structure.
<resources mapping="/resources/**" location="/resources/" />
Try this.
But I want to use Spring security.
I think i have to use DispatcherServlet and its configuration in web.xml
I am developing an application that is nor jsp nor jsf project, i am going to make all connection based on javascript/ajax/jquery via server communication.
Thus i do not want to map my xhtml pages to a controller.
But i have a single controller with #RequestMapping(/auth/login) i only want it to run when i request /auth/login this is not the problem, it is working excellently.
But when i use
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:META-INF/spring-servlet.xml
1
spring
/heythere/*
and call http://localhost:8080/app/myhtml.xhtml it tells me i have no mapping for this uri.
I do not want mapping, nor controller to run, only want to see the page.
But DispatcherServlet needs to map it, how can i tell DispatcherServlet not to map ordinary xhtml pages?
Option 1:
Inside your spring web mvc application context XML you should put something like:
<mvc:view-controller path="/myhtml.xhtml"/>
The downside is you'll have to do this per page.
Option 2:
Use a Resource Handler:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/static/**"/>
Your page would be visible like http://localhost:8080/app/static/myhtml.xhtml.
More info can be found in Spring's Doc.
I have a servlet that responds to a URL and then forwards to a JSP in a typical MVC pattern.
Many pages share the same page head so in the JSP there is an include to head.jsp
head.jsp is placed inside WEB-INF so that it cannot be accessed directly.
Now I find that I need to add some control to the head. Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
How can I forward from the JSP to a servlet without mapping that servlet to a URL as I do not want to give direct access to this servlet.
Or to put it another way is there a servlet equivalent of WEB-INF to hide it from direct access? So the servlet can only be called via an include?
Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
It's indeed possible to do this (using <jsp:include> or a small scriptlet that dispatches), but I'm not sure whether this is really the best approach. The Servlet would either write directly to the response or would put some data in the request scope that the JSP can pick up later.
Writing directly to the response is a bit debatable today and for the other approach you don't need a Servlet at all.
The idiomatic way is to use some helper bean that contains the logic. The original Servlet you mentioned can put this bean into request scope, or you can use the <jsp:usebean> tag. Reference the data the helper bean prepared via expression language or very simple scriptlets.
So the servlet can only be called via an include?
If you still want to go this route, there might be an option of securing the Servlet behind a role and then giving the head.jsp a run-as role in web.xml:
<servlet>
<servlet-name>headInclude</servlet-name>
<jsp-file>/WEB-INF/head.jsp</jsp-file>
<run-as>
<role-name>SYSTEM</role-name>
</run-as>
</servlet>
disclaimer: I have never tried this myself, just pointing in a possible direction.
I have a Spring application, which is running on Tomcat say at: http://example.com/foo/
DisplatcherServlet is mapped to app/*, for example, index page is:
http://example.com/foo/app/index.html
This is so because I have other servlets (for HttpRequestHandlers), e.g. mapped to service/*. This scheme is convenient to use because this way app/ and service/ can have different security settings.
What I want is http://example.com/foo to redirect to http://example.com/foo/app/index.html.
How do I achieve this?
In your web.xml, you can define a welcome file, seen when navigating to the root of the app:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
You can then make a tiny index.jsp that redirects to where you want:
<% response.sendRedirect("app/index.html"); %>
You can add the (tuckey) Url Rewrite Filter to your application.
It provides you the functionality to define URL rewrite rules in you Application (ingoing and outgoing).
So you can define a rewrite rule for \ that it rewrites to myApplication.startpage - or something else.
#see: http://mattgivney.blogspot.com/2010/07/how-to-url-rewrite-in-spring-mvc.html - for a short example