версия пружины в pom.xml
(<version>3.2.4.RELEASE</version>
) и упомянутые версии схемы в диспетчере-сервлете (например: http://www.springframework.org/schema/context/spring-contex-4.0.xsd
) должны быть согласованы
я удален <welcome-file-list>...
из web.xml
поэтому я добавил start()
-метод к HelloController
другие изменения, которые я сделал, прокомментированы в примерах исходного кода
, попробуйте следующее
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
}
}
Измените
, переименовав dispatcher-servlet.xml
в spring-servlet.xml
, нет необходимости указывать <context-param><param-name>contextConfigLocation...
в web.xml
если вы хотите сохранить «ваше» имя или следовать «вашим» соглашениям об именах, и это не работает, возможно, вы можете объявить contextConfigLocation
следующим образом
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:**/dispatcher-servlet.xml
</param-value>
</context-param>