Call index.html in Tomcat - spring

I'm trying to run an application in Tomcat. I create an application that generates a war file that I put on the Tomcat to run the application but when I try to run
http://localhost:8080/AppletTest/
it gives me error:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.28
And more detailed:
06-Nov-2015 16:35:07.052 WARNING [http-nio-8080-exec-51] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/AppletTest/] in DispatcherServlet with name 'mvc-dispatcher'
In the dir installation of tomcat my app resides on
~/apache-tomcat-8.0.28/webapps/
and the index.html and the other files are on the
~/apache-tomcat-8.0.28/webapps/AppletTest/WEB-INF/pages/index.html
.
I'm making any error?
My files:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0"
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">
<display-name>Test</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup></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>
</web-app>
mvc-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.something.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
</beans>

You have created a Spring application.
On your Web.xml you have mapped all requests (/*) to your Spring Servlet.
Now, you have to create a controller that will listen to your URL.
Example:
#Controller
public class IndexController {
#RequestMapping(value = "", method=RequestMethod.GET)
public String index(Model m) {
return "index/index";
}
}
Now this method will listen the {context}/ url and will return the index.html view.

Related

Why can't Tomcat and Spring see my jsp file?

I started learning Spring MVC.I connected all required dependencies with maven and downloaded Tomcat.But when i run Tomcat my jsp file is not opening. What the problem? Annotations seem to be correct
My applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package="com.ildar" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-cource-mvc</display-name>
<absolute-ordering />
<servlet>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Controller class:
#Controller
public class MyController {
#RequestMapping("/")
public String showMyView(){
return "first-view";
}
}
And when I run Tomcat , i just see eror 404 not found in browser

Postman returning 404 for REST API on correct path

I have created a Rest API. I was testing it on postman yesterday and it was working fine, today it keeps returning a 404, the file paths haven't changed so that can't be wrong. I am using tomcat, and my xampp is on.
It postman is returning the correct data for another application I have.
The path I am trying to test is
http://localhost:8080/Assignment2C/breweries
the method I'm trying to test is
#RestController
#RequestMapping("/breweries")
public class Breweries_Controller {
#Autowired
Breweries_Service service;
#GetMapping(produces = MediaTypes.HAL_JSON_VALUE)
public Resources<Breweries> getAllBreweries() {
List<Breweries> allBreweries = service.getAllBreweries();
for (Breweries b : allBreweries) {
int id = b.getResourceId();
Link self = linkTo(this.getClass()).slash(id).withSelfRel();
b.add(self);
linkTo(methodOn(this.getClass()).getBrewerie(id));
}
Link link = linkTo(this.getClass()).withSelfRel();
Resources<Breweries> result = new Resources<Breweries>(allBreweries, link);
return result;
}
I am using JPA to connect to the database.
the project structure is
the config file is
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="Assignment2C" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
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">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sd4-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
try to give path to the get mapping also
#GetMapping("/getAll")
you url will should look like
this:
http://localhost:8080/Assignment2C/breweries/getAll
Or
http://localhost:8080/2C/breweries/getAll
if i set the base_package to
<context:component-scan base-package="main" />
it works

WARNING : No mapping found for http request with uri [/Authentication/] in dispatcherservlet with name 'spring' [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I'm trying to make a simple web application which has a login and a welcome page using Spring MVC. The code is as follows:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</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.test"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Controller.java
#Controller
#RequestMapping("/Authentication")
public class TestController{
#RequestMapping(value="/")
public String Login(){
return "Login";
}
#RequestMapping(value="Authenticate", method=RequestMethod.GET)
public String Authenticate(){
//Authenticates and returns "Welcome"
}
}
The project name is Authentication. There are Login.jsp and Welcome.jsp under /WEB-INF/jsp/.
However, when I'm trying to run the project, I am getting HTTP Status 404 error and the following warning:
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING : No mapping found for http request with uri [/Authentication/] in dispatcherservlet with name 'spring'
Why am I getting this warning even though the mappings look fine?
You are not specifying where the spring-servlet will be located. Add these to your web.xml file:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>

url-pattern doesn't work in spring mvc application

Spring 3.2 in apache tomcat
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">
<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/rest-servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
rest-servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<context:component-scan base-package="com.*" />
</beans:beans>
My Controller:
#Controller
#RequestMapping(value = "/rest/person")
public class PersonController {
#RequestMapping(value = "/test")
#ResponseBody
public String getTest() {
return "Test controller";
}
}
When i call /localhost:8080/rest/person/test, i get 404 error (page not found).
However, when i change the url-pattern in web.xml file to:
<url-pattern>/*</url-pattern>
it works fine.
How can i make the first url-pattern work??
Remove rest from your RequestMapping, e.g. change it to
#RequestMapping(value = "/person")
The reason for this is that your url-pattern already contains rest/*, so what you were doing was to map to the URL rest/rest/person/test and not rest/person/test.

Spring 404 error

I am trying to run spring application, but i am getting the 404 error. Can anyone please help?
I take this code from mkyong site. The example he given is working but i tried to do everything from scratch it is not working.
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/welcome")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
hello.jsp
<html>
<body>
<h1>Message : ${message}</h1>
</body>
</html>
mvc-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.mkyong.common.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Add a * to the servlet's URL pattern in your web.xml:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
use #RequestMapping("/welcome") on your method instead of class

Resources