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.
Related
I am very new to Spring MVC and have been following tutorials but they don't seem to be working.
I can load my index.jsp file fine but when I try to map it to the Dispatcher Servlet I keep getting resource not found errors.
HelloWorldController:
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC";
return new ModelAndView("hellopage", "message", message);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<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>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
my jsp files hellopage.jsp and welcomepage.jsp are in the folder jsp under my WEB-INF folder.
Index.jsp:
click|
click
I am trying to map hello.html to the HelloWorldController.java.
I would greatly appreciate any help as I am having problems mapping the first Spring MVC application.
The problem is with your servlet mapping, try changing it in your web.xml to:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
your url-pattern is set to *.html. Therefore only requests that ends with ".html" will be dispatched to spring mvc.
Add
<url-pattern>*.jsp</url-pattern>
to your servlet-mapping in web.js.
Can you try #RequestMapping(value="/hello",method = RequestMethod.GET ) ?
Your url pattern in the servlet-mapping is wrong.
You're saying go to the dispatcher servlet when the URL matches this:
*.html
So when you use "hello.html" the DispatcherServlet gets triggered.
However, there is no mapping for hello.html. There's only one for hello. HTML is static content, and it's not typical to serve that through a controller.
Try this:
Change the url-pattern to this:
<url-pattern>/</url-pattern>
Change the hello.html to hello.jsp
Change your method signature to return a string, and add the Model to the method.
#RequestMapping("/hello")
public String helloWorld(Model model) {
return "hello";
}
Your anchor tags should then point to the correct mapping. They'll look like this:
click|
So how does this work? These two pieces:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The above is a pretty standard. In the servlet mapping, it's saying that anything that matches that URL pattern should hit the servlet named spring you defined in the web.xml.
The view resolver is looking in the /WEB-INF/jsp/ directory for files ending in .jsp. In other words, Spring will look for /WEB-INF/jsp/<string>.jsp. So when you return a String hello in the method, it is actually going to attempt to resolve /WEB-INF/jsp/hello.jsp.
If you understand the above, you may have realized you can serve *.html files this way also. They would require their own view resolver, however.
I am building an angular 2 app that has to use spring mvc based rest api at the backend. I am using maven, not using spring-boot, and want to map the index.html of angular2 to the base URL of the deployed WAR file. I am seeing the index.html to be present in the MyApplication named folder in webapps folder of tomcat, but somehow trying to access the site via the base URL gives no resource available. Could somebody help me. This is my layout.
web.xml
<display-name>MyApplication</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
mvc-dispatcher-servlet.xml
<context:annotation-config/>
<context:component-scan base-package="com.rochak.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/angular/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
MyController.java in com/rochak/controller
#Controller
public class MyController {
#RequestMapping(value="*")
public String getIndex(ModelMap model){
return "index";
}
}
and angular folder lies in WEB-INF folder with it's index.html
Why is index.html not being found by spring mvc?
Change your 'url-pattern' to something like '/server/'. It would work.
I have made a small spring mvc app. In my controller i have 2 methods which returns the names of the jsp files in the web-inf folder.
Now the application works perfectly, but if i try to add a url path it doesn't work.
What i mean is something like this:
#Controller
#RequestMapping("/start") //if add this it doesn't work
public class SalesController {
#RequestMapping(value = "/greeting")
public String sayGreeting(Model model) {
model.addAttribute("greetingMsg", "Hello Spring");
return "welcome";
}
#RequestMapping(value = "/hello")
public String getHello(Model model) {
model.addAttribute("greeting", "Yo man");
return "hello";
}
}
Here is my servletConfig configuration
<mvc:annotation-driven />
<context:component-scan base-package="com.myCompany" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
If i give the path "myApplicationName"/start/greeting it give error. But if i remove start it works.
What seems to be the problem here?
Thank you
Update:
Below is my web.xml configuration
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Add /.html* in your URL Pattern:
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servletConfig.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/*.html</url-pattern> </servlet-mapping>
I'm creating my first project in Maven, and already have a noob problem.
In my WEB-INF folder, I have another folder called "classes", in that folder I have a view called "view.jsp".
When I try to access the URL
http://localhost:8080/ProjectenIIJavaWeb/classes/view
I get a Http 404 not found error.
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
You have configured the DispatcherServlet only for the url pattern *.htm. So you have to call a URL with that pattern, otherwise there is no servlet which matches your request.
As the name says, the folder "classes" is for Java classes. Resources should be in the root of the WAR file.
You don't call your view, you define request mappings and let Spring MVC handle the view resolution. I suggest to read the Spring MVC documentation (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc)
In case you plan to keep your jsp's(without exposing the detailed path of actual file) inside WEB-INF this should help
<bean id="jspViewResolver" 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>
Detailed example: http://www.mkyong.com/spring-mvc/spring-mvc-internalresourceviewresolver-example/
My web.xml contains
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
resources/index.html
</welcome-file>
</welcome-file-list>
</web-app>
The resources/index.html references to other static resources like images, js, css etc. stored in resoruces directory by relative paths.
When I put http://localhost/MyProject/ in browser, it showed up index.html but didn't get the css and javascripts.
However, if I put http://localhost/MyProject/resources/index.html in the browser, everything shows up correctly.
So, the question is how can I let the welcome page served in the url as the path given in the <welcome-file>, e.g. /resources/index.html.
If it can't be done in the <welcome-file list>, what other configurable method should I use.
I tend not to redirect to /resources/index.html by adding another html or by doing it programmatically in a Servlet controller.
It seems you are using Spring and having problems with static content.
Try looking at this link
It explains how to proceed in this case...
Pay atention to the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
It maps your resources folder (containing the css, javascript and image files) to a special handler of Spring.
Update:
In your servlet-context.xml file you can add this line
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Resolves view names to protected .html resources within the /WEB-INF/resources/ directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/resources/"/>
<property name="suffix" value=".html"/>
</bean>
that says that you don't have to use the 'index.jsp' properly. This way, you will map a view to the "/" access. Summarizing, this way the user enters in 'http://localhost/MyProject/' and sees your index.html and sees the effects of css and javascripts.
PS.: - This configurations only works on Spring 3+
- Prefer naming your files to '.jsp' and not '.html'... it is simpler to map.