Reputation: 606
I am having trouble executing basic spring mvc application. I have tried all possible solutions and finally wanted to get this resolved. I don't want to create a new workspace and do it again. I wanted to get this working. Here is my GitHub url for the project. I am landing on correct page (index.jsp).
I tried all these
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
@RequestMapping
from method to class and by changing it from
(/welcome)
to (value = "/welcome", method = {RequestMethod.GET,RequestMethod.POST})
Sometimes I see no-mapping-found-for-http-request-with-uri-in-dispatcherservlet-with-name
error in console and sometimes its nothing.
I know there are many similar questions out there, I have gone through lot of questions and started this new thread. My problem is
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" version="3.0">
<display-name>BasicSpringMVC1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>basicspringmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>basicspringmvc</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>/test.jsp</url-pattern>
<url-pattern>/test.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
basicspringmvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.basicSpringMvc1.controller" />
<mvc:annotation-driven/>
<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>
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BasicSpringMVC1</groupId>
<artifactId>BasicSpringMVC1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
SpringHelloWorld.java
package com.basicSpringMvc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpringHelloWorld {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from SpringHelloWorld.java **********</div><br><br>";
return new ModelAndView("welcome", "message", message);
}
}
Project Structure
Upvotes: 0
Views: 194
Reputation: 185
Notice that, in your servlet mapping:
<servlet-mapping>
<url-pattern>/welcome.html</url-pattern>
</servlet-mapping>
you have added url-pattern for welcome.html
.
But your controllers' request mapping is set as @RequestMapping("/welcome")
.
Either add url mapping for /welcome
:
<servlet-mapping>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
so that in can get mapped to your controller.
Or change the request mapping of your controller to @RequestMapping("/welcome.html")
so that the controller get mapped to welcome.html
.
Upvotes: 1
Reputation: 891
Check if this helps!. Change the servlet-mapping
entry in Web.xml to what is below. You have to avoid putting lot of url-pattern
in your web.xml
<servlet-mapping>
<servlet-name>basicspringmvc</servlet-name>
<url-pattern>*htm</url-pattern>
</servlet-mapping>
then try this http://localhost:8080/BasicSpringMVC1/welcome.htm
Upvotes: 0