I searched all over the site and nothing suggested worked!
I'm tryng to make a spring-mvc and angularJS application; This is my project structure:
angularState
-src/main/java/it.controller.DefaultController.java
-src/main/webapp
--index.jsp
--WEB-INF
---dispatcher-servlet.xml
---web.xml
---static
----default.html
so in my web.xml I have:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
in the dispatcher-servlet.xml:
<context:component-scan base-package="it.controller"></context:component-scan>
<mvc:annotation-driven />
<context:annotation-config />
<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
in the index.jsp:
<% response.sendRedirect("default"); %>
then in the controller:
#Controller
#RequestMapping("/default")
public class DefaultController {
public String showDefault(){
return "/static/default.html";
}
}
I've always used jsp and everything was fine; now I've tried every single question of the site:
I've tried
-mvc:default-servlet-handler
-InternalResourceViewResolver with prefix "/WEB-INF/static" and suffix ".html" or even suffix void
-InternalResourceViewResolver void and the controller method returning "/WEB-INF/static/default.html"
NOTHING!
nothing showed the default.html. why that .html file is so complicated!
This is because normally *.jsp style uri patterns are handled by the servlet container and in your case *.html is not being handled by the container and instead the path is being delegated to Spring MVC which does not know how to render these extensions
As a quick fix you can try below code in conf/web.xml
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Or
Content negotiating view resolver can also be used in place of InternalResourceViewResolver(I Guess you might be using this) which return a suitable view based on file extension declared in “mediaTypes” property)
Example
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html"/>
</map>
</property>
For anyone who will google, here the answer.
the problem was pretty simple: All I was wrong was the controller method; the original controller method was:
#Controller
#RequestMapping("/default")
public class DefaultController {
public String showDefault(){
return "/static/default.html";
}
}
the correct version must be:
#Controller
#RequestMapping("/default")
public class DefaultController {
#RequestMapping(method=RequestMethod.GET)
public String showDefault(){
return "/static/default.html";
}
}
because every page must have a GET access, more than all, the homepage!
Because of that missing #RequestMapping on the method, every config I tried gave me errors on the .html files. As I sayd, .html files only needs the
<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
to be configured because they're static resources and they don't need the help of the servlet. Hope this will help someone, thanks to everyone who tried.
Related
When I am trying to execute #RequestMapping("/showForm") I am facing an error.
I think my code seems fine, I am simply returning new String with the name of my JSP file - "mainmenu.jsp". I have this folder the folder JSP in the right place.
The error:
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one
exists.
Where can be the problem?
<mvc:annotation-driven />
<context:component-scan
base-package="com.crunchify.controller" />
<mvc:default-servlet-handler />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/mainmenu.jsp</url-pattern>
<url-pattern>/mainmenu.html</url-pattern>
<url-pattern>/processForm.jsp</url-pattern>
<url-pattern>/processForm.html</url-pattern>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/main-menu.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
The actual request:
#RequestMapping("/showForm")
public String helloWorld() {
return "mainmenu";
}
Your deployment descriptor(web.xml) does not have mapping for the url you are trying to access. Either add "/showForm" in url mapping for dispatcher servlet or use a wild card "/" in your url mapping for your dispatcher servlet. like,
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
As web.xml is the entry point, there should be url mapping.
(And also you can map different url for different dispatcher servlet. In other dispatcher servlet you can use another view resolver.)
Hope <url-pattern>/</url-pattern> it will work for you.
Go to your apache web server config, its probably in ProgramFiles > Apache Tomcat folder
Go to tomcat>conf folder
Edit server.xml
Search "Connector port"
Look for the connection timeout, if its set to -1 then that is the issue
Change it to "20000", save the file. Restart the Apache Service
Problem will be fixed.
I'm a newbie to web development as well as Spring MVC. I'm testing on a silly example for the controllers, however, it seems that the controller has never been called, or at least it doesn't work as expected.
The goal is to click on a hyperlink in the "test.jsp" page and direct to "customers.jsp" page, with the controller being called, so that a message will be passed to the "customers.jsp" page.
Now I have the "customers.jsp" page showing correctly but with no transferred message displayed. The URL is http://localhost:8080/WebProject/view/customers.jsp.
Could you please help me find out the problem? I suppose it's because of the URL mapped to the controller. But is there a way that I can check the right URL to map for controllers? Thanks!
* updates at the end *
The project structure is like this:
--webapp
|--view
|--*.jsp
|--WEB-INF
|--*.xml
Here is my code:
test.jsp
<a href='<c:url value="customers.jsp"/>'>show customers</a>
or
show customers
both works properly.
customers.jsp
<h2>return: ${list}</h2>
Controller:
#Controller
#RequestMapping("/customers")
public class MenuController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
and the configuration:
web.xml
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
myDispatcher-servlet.xml
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
UPDATE for those who might see this post later :
There is indeed a problem of url mapping as indicated by #javafan and #Beri. But after changing the url to show customers I still got Error 404 at http://localhost:8080/WebProject/customers. This is in fact caused by a package scanning error, so the Controllers cannot be found. To solve this, simply change the line in the "myDispatcher-servlet.xml" file:
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
to
<context:component-scan base-package="controller" />
<context:component-scan base-package="model" />
Hope it helps :)
Javatan is right:
Try
<protocol>/<host>:<port>/WebProject/customers
Inside InternalResourceViewResolver bean definition you define prefix and suffix, those values are used to find jsp files in yur application. And have nothing in common with urls that your application has created.
Your url paths are defined inside controllers( not inside InternalResourceViewResolver).
So after application name you are using paths defined inside controllers.
You can also define #RequestMapping("/customers") on a method:)
Check if you have in your bean definition line, that looks like this:
<context:component-scan base-package="com.project.web.controllers"/>
This definition will scan for all classes inside com.project.web.controllers package (and subpackages) to find classes marked with #Controller annotation. You can check for more here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html, chapters:
17.3.1 - how to register a controller
17.2 -how web.xml looks like
I hope this will help.
Ok, I have run local example with JSP:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Controller:
#Controller
public class MenuController {
#RequestMapping( value="/customers", method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
You can use this code to get the url of the server:
"show customers"
And you are mapping the Controller to /customers
so the url should be:
http://localhost:8080/WebProject/customers/customers.jsp
You are directly calling customer.jsp. Instead you have to call controller which in turn pass the request to customer.jsp. in HREF attribute, you need to mention value which is given in #RequestMapping annotation in your controller.
For more detailed example, visit my blog Here
I'm creating a Spring MVC application and can't figure out why I get 404 error when launch my application. I want to see welcome page but it doesn't show. What is the issue?
In /WEB-INF/view there is index.jsp file with simple Hello text.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
grun-servlet.xml
<context:component-scan base-package="Controller"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
Controller
#Controller
#RequestMapping(value="/")
public class AController {
#RequestMapping(method=RequestMethod.GET)
public String welcome() {
return "index";
}
}
I think the problem is this below bit in grun-servlet.xml
base-package="Controller"
Unusual to name a package as Controller with the uppercase 'C'. So unless you have
package Controller;
at the beginning of your AController.java file, that is a problem.
You can also try the following
WEB-INF flolder in generally where runtime libraries and class go. Create view folder inside Webcontent folder and provide path as
<property name="prefix" value="/view/"/>
Wecontent is your root folder for all the resources you want to expose. Reason was doing above is that as Webcontent is the root folder to expose web resources directories inside it can be specified relative to this. So Any resource abc under folder xyz can be accessed using /abc/xyz.
Nothing is wrong with your configuration, Just add Index.jsp as a welcome file on you web.xml and it should works
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Please change your web.xml to this
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Hope it will work fine
2 possible things:
Did you try putting the request mapping at the method level instead of at the Controller level?
#Controller
public class AController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index";
}
}
Try adding the below to your grun-servlet.xml:
<context:annotation-config />
<context:component-scan base-package="com.xxx.yyy" />
where "com.xxx.yyy" is the base package to which your AController belongs to.
I am getting the following error:
No mapping found for HTTP request with
URI [/my-app] in DispatcherServlet
with name 'web'
My web.xml looks like:
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
And my web-servlet.xml looks like:
<bean name="myController" class="com.app.web.MyController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Any help / explanation would be great. Also, what should the view parameter be to new ModelAndView(?) in the controller?
My goal is to be able to hit http://localhost:8080/my-app and be routed to MyController which would then load a given jsp.
Your configuration looks fine to me. In your MyController, make sure you have a request mapping for my-app, like this:-
#Controller
public class MyController {
#RequestMapping(value="/my-app", method=RequestMethod.GET)
public String mainPage() {
return "index";
}
}
When you call http://localhost:8080/my-app, the server will return the index.jsp from /WEB-INF/jsp/ folder.
Looks like DispatcherServlet is trying to process the request for /my-app, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.
You might have something like this?
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>/*</url-pattern> </servlet-mapping>
Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>*.do</url-pattern> </servlet-mapping>
or change /* to /
Hope that helps.
We are in the process of migrating a jsp-only application to Spring-MVC. For various reasons we can't change the extension of the current pages. (calls to login.jsp need to handled by a spring controller that will use a jsp file as view).
We are doing this iteratively, so some pages need to stay jsp files (calls to welcome.jsp won't be handled by a controller).
To do that I mapped both the DispatcherDervlet and the HandlerMapping to *.jsp, and configured the JstlView in the standard way.
Unfortunately, if I browse to //login.jsp I get an error saying
<No mapping found for HTTP request with URI [/<context>/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'spring'>
It all works if I change .jsp to any other extension in DispatcherServlet and HandlerMapping.
web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
spring-servlet.xml:
<!-- View resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- URL Mapping -->
<bean id="publicUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/login.jsp" value-ref="loginController"/>
</map>
</property>
</bean>
Thanks a lot.
UPDATE: I just verified that if I rename my .jsp files to something else (.jst) and update the viewResolver accordingly, than it all works. Apparently if the view is resolved to a file with extension .jsp, spring tries to forward the view to another controller.
[blatantly stolen from http://forum.springsource.org/showthread.php?13263-Using-.jsp-extension]
This worked for me. Try adding this to your web.xml file:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/WEB-INF/jsp/*</url-pattern>
</servlet-mapping>
Please note that even with the information in the link, I don't understand why this is helping. If some Spring expert could drop by and explain it, I'd love to know.
I also can't guarantee that there are no potential security/reliability issues this could create, so use at your own risk.
if it's really not working with .jsp extensions (although i can't personally see any reason for that), you could try using http://tuckey.org/urlrewrite/ to do a rewrite of the urls first.