I start Spring Mvc with a simple hello world application, but not working.
Use Eclipse editor and run on Tomcat Server.
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hello</groupId>
<artifactId>SpringMavenHello</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMavenHello Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMavenHello</finalName>
</build>
</project>
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">
<display-name>Archetype Created Web Application</display-name>
<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/dispatcher-servlet.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoadListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-contex-4.0.xsd
http: / www.springframework.org / schema / mvc">
<mvc:annotation-driven />
<context:component-scan base-package="com.hello" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
index.jsp
<html>
<body>
<a href="hello">click here</a>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Spring MVC</h1>
</body>
</html>
HelloController
package com.hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/hello")
public String sayHello() {
return "welcome"; // nome pagina
}
}
When I run on the server and http://localhost:8080/SpringMavenHello I receve this error :The requested resource is not available.
Help me!! Please! Thanks!
the spring-version in pom.xml
( <version>3.2.4.RELEASE</version>
) and the referred scheme-versions in dispatcher-servlet (for example: http://www.springframework.org/schema/context/spring-contex-4.0.xsd
)should be consistent
i removed <welcome-file-list>...
from web.xml
therefore i added start()
-method to HelloController
other changes i made, are commented in source-code-examples
please try the following
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"
>
<display-name>Archetype Created Web Application</display-name>
<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/dispatcher-servlet.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoadListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<!--not needed anymore -->
<!--welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list-->
</web-app>
dispatcher-servlet.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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-contex.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!-- above schemaLocation for mvc edited -->
<mvc:annotation-driven />
<!-- .** added to also scan sub-packeges -->
<!-- annotation-config="true" added allow annotation-based-configuration -->
<context:component-scan
base-package="com.hello.**"
annotation-config="true"/>
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property
name="prefix"
value="/WEB-INF/jsp" />
<property
name="suffix"
value=".jsp" />
</bean>
</beans>
HelloController
package com.hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
/**
* the annotation {@link RequestMapping} can be shortened/replaced with
* {@link GetMapping}
*
* @see {@link HelloController#start()}
*/
// method specified
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return "welcome"; // nome pagina
}
@GetMapping("/")
public String start() {
return "index"; // nome pagina
}
}
Edit
by renaming your dispatcher-servlet.xml
to spring-servlet.xml
, there is no need to declare <context-param><param-name>contextConfigLocation...
in web.xml
if you want to keep "your" name or follow "your" naming-conventions and it does not work, it might be an approach to declare the contextConfigLocation
like this
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:**/dispatcher-servlet.xml
</param-value>
</context-param>
Another Edit
could you solve your problem?
some steps are routine for me and i dont think of them
to start a "basic" dynamic web project with maven and spring, i do the following in eclipse:
i open project-properties, go into project facets and click on "convert to faceted form
i ensure "Dynamic Web Module", "Java" and "JavaScript" are selected, also ensure eachs version to use dont click apply and dont click apply and close; you have to click " Further configuration available "
in the following dialog i change Content directory to "src/main/webapp"
i stay in project properties, but i go into "deployment assembly" and click "add"
after i clicked add, i choose Java Build Path Entries in the upcoming dialiog
i select Maven Dependencies in the next dialog and click finish
as far as all of this is done, im going to change my pom and add the required dependencies
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.