I am working on a Spring MVC project incorporating Tiles.
Here is the current urlPattern in the web.xml.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In the spring configuration file, currently the only view resolver is.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="views" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs.xml" />
In my controller, the only way the request mapping works is if I enter in /sample.html which routs to the proper controller. What I need to do is have the current view /sample to map to the proper controller. Is there a way to do this. Would internalviewresolver or an entry change in the views.properties file be the answer? Thanks.
No, you have to change the servlet url-pattern. Map it to /
Related
I had made a basic application on spring mvc framework.
when i write the following url pattern on web.xml:
<servlet-mapping>
<servlet-name>springxml</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
and runs the application (using ../SpringMVCXML/welcome.jsp) , it displays HTTP Status 404 error.
When I changes the url pattern other than .jsp, application is running fine.
Why Application is not running on .jsp url pattern?
I had used following java class act as controller.
#Controller
#RequestMapping(value="/welcome",method=RequestMethod.GET)
public class ControllerHello {
#RequestMapping(method=RequestMethod.GET)
public String printHello(ModelMap map) {
map.addAttribute("message", "Hello Spring MVC Framework");
return "hello";
}
}
Also, my springxml-servlet.xml had following code:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Assuming you have also springxml servlet is also serving other views than *.jsp (you might have other servlet-mappings in your web.xml), the situation is as follows:
You registered a controller for the path /welcome, not for /welcome.jsp. Therefore, /welcome.jsp is not mapped to a Spring mvc controller.
The /welcome HelloController will give you the String output hello.
/welcome.jsp will give an error 404, since it is in WEB-INF and there is no Spring MVC controller for that url.
The org.springframework.web.servlet.view.InternalResourceViewResolver is meant to put your jsp files inside /WEB-INF. Look at http://www.mkyong.com/spring-mvc/spring-mvc-internalresourceviewresolver-example/ for a simple explanation for what the InternalResourceResolver does. Basically, it enables Spring MVC to use a jsp, that is not in your public resources, as a view.
It is not a mechanism to register these jsps as valid urls.
The urls are determined in the requestmappings.
you probably do not have any spring controller mapping to this URL /welcome.jsp
what version of spring you are using? if you are using spring 2.5 or above, try following code
#RequestMapping(value = "welcome.jsp")
public String welcomeJSP(){
return "welcome";
}
I just switched from Striped to Spring but i'm having issues with my very first project,
Basically i get the 404 from the server.
Strange enough, i have followed one by one all the steps in my book.
I use Eclipse, Tomcat 6 and Spring 2.5
The structure of my project is like so:
src>
controllers(package)>SpringTestController(implements controller).....then
......web content>jsp(folder)>hello.jsp.....then....web-content>web-inf>SpringTest-servlet.xml and web.xml
inside lib i have the 9 necessary jars.
my controller:
public class SpringTestController implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
return new ModelAndView("jsp/hello.jsp");
}
}
my SpringTest-servet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/hello.htm" class="controllers.SpringTestController"/>
</beans>
my web.xml(without header to save space)
<servlet>
<servlet-name>SpringTest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTest</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Where do you think the problem could be?
I have been trying to look around the files but beside web.xml "where i dont see any abnormality" i'm very new to this flow structure so i really cannot get where the problem is.
Thx for your time
I'm suspicious of the practice of using the name attribute of bean to specify URL paths - while I'm sure it's probably possible, my answer will tell you how to do it using more traditional means.
First of all, here is the new SpringTest-servlet.xml:
<bean id="helloController" class="controllers.SpringTestController" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">helloController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
You'll probably notice a few things. I'm using the id attribute of bean to define your controller so it can be referenced elsewhere (in the urlMapping bean as you're about to see).
I define a urlMapping bean, which does exactly what you think it does - maps requests (e.g. /hello.htm) to a controller bean.
I've also used viewResolver to map views names to view files, however that is a personal preference thing. Since I'm now using a view resolver, your controller looks like this:
public class SpringTestController implements Controller {
#Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return new ModelAndView("hello");
}
}
I don't need to put the path to the view because the viewResolver prepends it with "/WEB-INF/jsp/" and adds ".jsp" to the end. You can change the prefix to wherever you store your view files, or you can not use it at all. It's a personal preference thing, though I like to use it :)
Sorry if this answer didn't conform to your style - I tried to get it working your way and couldn't, so this is how I normally set up a Spring project (if I'm not using annotations).
Hope this helps.
In my application I'm using spring MVC(3.0.5) architecture along with BIRT reporting framework.
I'm trying to serve all requests including the static resources like css, js, html and image files using the spring DispatcherServlet.
For this purpose I added the following entries to my web.xml
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...............
...............
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This will direct all request to the DispatcherServlet and in my context file I added
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/docs/**" location="/docs/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/themes/**" location="/themes/" />
so that these resources will be loaded from the file system.
These configurations are working fine. But I'm facing issues with my BIRT reporting engine now.
The BIRT reporting engine uses some jsp files located in a folder called webcontent which is located at the root of the application. Since we are directing all request to DispatcherServlet even the request for these jsp pages are going to the spring servlet. As I understand from some posts the jsp files are normally handled by org.apache.jasper.servlet.JspServlet which is registered in the Apache Tomcat's web.xml file and it has a servlet mapping as follows
<!-- The mapping for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
What are the changes that I should make in my servlet mapping to work in this environment? I need the jsp files to be handled by the default jsp servlet not by the spring servlet. How can I achieve this?
For this post I understood that the second priority in servlet matching is for the url prefix, so my url pattern / for spring servelt is overriding the default jsp servlet mapping, Is this assumption correct? If it is correct then how to overcome this?
Thank you.
The typical mapping of DispatcherServlet is <url-pattern>/</url-pattern>. In this case it still handles all requests except for requests handled by other servlets (in particular, requests to *.jsp), so that it should solve the problem.
I have a Spring MVC Web app that I'd like to show a simple Welcome Page (index.html). On that page, I just to have a 2 href links: one to bring me to the Login Page that is then implemented using Spring Security (2.5.6) and Hibernate 3 and the other to a Registration Page for new users.
However, the problem is that Spring Security automatically loads my login page each time and does NOT load the index.html page where I have coded the 2 links to forward me to either login or registration. I am brought to the login page which works fine. However, I never get to show the initial index.html page of my web application.
Can anyone shed light on how to prevent Spring Security from overriding the 'Welcome Page' with it's Login Page.
Many thanks.
Here is my Spring Security set up in web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring-beans.xml
WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
There's nothing wrong with your web.xml file, you need to show us your WEB-INF/spring-security.xml file.
If you keep getting directed to the login page, chances are you mess up the intercept-url pattern that causes your welcome page to be caught by Spring Security for further authentication before displaying it.
This is an example of the intercept-url tags that you will find in your WEB-INF/spring-security.xml file:-
<http auto-config="true" access-denied-page="/accessDenied.jsp">
<intercept-url pattern="/login.jsp*" filters="none"/>
<intercept-url pattern="/admin/searchUsers.do" access="ROLE_ADMIN" />
<intercept-url pattern="/**.do" access="ROLE_USER,ROLE_ADMIN" />
<form-login authentication-failure-url="/login.jsp?login_error=1" default-target-url="/home.do"/>
<logout logout-success-url="/home.do"/>
</http>
use
<form-login login-page="/login.jsp" />
Controller should handle user's request and in your case no controller which mapped to this URL. When controller found, it performs some logic and returns view name which will be used to represent server's response. So, view name translator called only after controller and only for deduce full path to particular JSP file.
<mvc:view-controller path="/" view-name="index"/>
Try to add
I seem to be getting the following error when I try to access a Remote Java class (on Spring/BlazeDS) from the Flex/Cairngorm application. I am going crazy at the moment trying to see what is wrong - any help would be greatly appreciated - thanks Mike.
**Error: C0007E: RemoteObject not found for mycomponentsService
at RemoteObjects/getService()
at com.adobe.cairngorm.business::ServiceLocator/getRemoteObject()
at com.nomura.dashboard.client.business::DashBoardDelegate()**
All my config files are below:
Cairngorm - BusinessDelegate.as
this.service = ServiceLocator.getInstance().getRemoteObject("**mycomponentsService**");
Cairngorm - Services.mxml
mx:RemoteObject id="mycomponentsService"
destination="remotecomponentService"
showBusyCursor="true">
Spring/BlazeDS - application-config.xml
flex:remote-service ref="remotecomponentService"
bean id="remotecomponentService"
class="com.mycompany.dashboard.server.dao.ComponentsDAO"
Spring/BlazeDS - services-config.xml
channel-definition id="myamf" class="mx.messaging.channels.AMFChannel"
endpoint url="http://localhost:8080/dashboard-server/spring/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"
The web.xml also contains Spring references - see below
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<display-name>dashboard-server</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map /spring/* requests to the DispatcherServlet -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
</web-app>
Can we see your web.xml also please? I am surprised to see the word "spring" in the endpoint URL. My endpoints have always looked like
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
E.g.: I think your services-config.xml should look more like this.
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}/dashboard-server/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
I would also suggest not hardcoding the end-point URL so much. Just go with
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint class="flex.messaging.endpoints.AMFEndpoint"
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" />
</channel-definition>
Update:
OK, so your web.xml looks OK, as does having the spring in your URL. What I don's see in your Spring configuration file (application-config.xml) is the Spring URL mapping. For example, in my Spring config files, in addition to the the bean definitions, there is a mapping. E.g.:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/histogram/**=bean.HistogramController
/counter/**=bean.CounterController
</value>
</property>
</bean>
<bean id="bean.HistogramController" class="ch.comp.app.HistogramXportController" />
<bean id="bean.CounterController" class="ch.comp.app.CounterXportController" />
(I have one app that is Spring-based, and another that uses BlazeDS, but not both...so I might be missing something. That said, what I'm asking still should be valid. In theory. But take it with a grain of salt.)
Maybe some some super basic debugging is in order. Can you check to see if the calls to your server are returning HTTP 404 or not on theses problem endpoints? A couple ways to do this:
Check the access logs for GET /dashboard-server/spring/messagebroker/amf. What is the HTTP status code for these requests? (Free, easy, no new tools.)
If you are using FireFox as a browser, add the Tamper Data plug in. You don't have to tamper with the data, but it shows you what is being called, what is returned, and all the HTTP headers.
Use a full on Flash/Flex oriented protocol sniffer tool, like Charles Web Debugging Proxy.
It will very helpful to narrow down the problem to know if where these requests are failing on the communications stack.
I have attached web.xml below. In terms of the word "Spring" - I am using the standard BlazrDS/Spring integration WAR file which requires "spring" to be there. The bean id="remotecomponentService" is acutally a Spring bean.
Are you saying even with the BlazeDS/Spring WAR I can use your solution above?
The web.xml also contains Spring references - see below
<web-app version="2.4">
<display-name>dashboard-server</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map /spring/* requests to the DispatcherServlet -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
</web-app>