Причина 404 статуса : Ваш Dispatcherservlet не может найти ресурс, то есть user.jsp
.
Не забудьте удалить это ниже из сети. xml:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
......
</welcome-file-list>
Поскольку это мешает диспетчеру сервлету для разбора представления.
Следуйте этому примеру для простого проекта spring- mvc.
-> Создайте простой веб-проект. Примечание: для maven структура отличается. Структура проекта для простого веб-проекта.
spring-mvc-demo
│
│
├───src
│ └───com
│ └───thp
│ └───project
│ └───vintud
| └───controller
│ DemoController.java
│
└───WebContent
├───META-INF
│ MANIFEST.MF
│
└───WEB-INF
│ applicationContext.xml
│ web.xml
│
├───lib
│ javax.servlet.jsp.jstl-1.2.1.jar
│ javax.servlet.jsp.jstl-api-1.2.1.jar
│ spring-aop-5.0.2.RELEASE-javadoc.jar
│ spring-aop-5.0.2.RELEASE-sources.jar
│ spring-aop-5.0.2.RELEASE.jar
│ spring-aspects-5.0.2.RELEASE-javadoc.jar
│ spring-aspects-5.0.2.RELEASE-sources.jar
│ spring-aspects-5.0.2.RELEASE.jar
│ spring-beans-5.0.2.RELEASE-javadoc.jar
│ spring-beans-5.0.2.RELEASE-sources.jar
│ spring-beans-5.0.2.RELEASE.jar
│ spring-context-5.0.2.RELEASE-javadoc.jar
│ spring-context-5.0.2.RELEASE-sources.jar
│ spring-context-5.0.2.RELEASE.jar
│ spring-context-indexer-5.0.2.RELEASE-javadoc.jar
│ spring-context-indexer-5.0.2.RELEASE-sources.jar
│ spring-context-indexer-5.0.2.RELEASE.jar
│ spring-context-support-5.0.2.RELEASE-javadoc.jar
│ spring-context-support-5.0.2.RELEASE-sources.jar
│ spring-context-support-5.0.2.RELEASE.jar
│ spring-core-5.0.2.RELEASE-javadoc.jar
│ spring-core-5.0.2.RELEASE-sources.jar
│ spring-core-5.0.2.RELEASE.jar
│ spring-expression-5.0.2.RELEASE-javadoc.jar
│ spring-expression-5.0.2.RELEASE-sources.jar
│ spring-expression-5.0.2.RELEASE.jar
│ spring-instrument-5.0.2.RELEASE-javadoc.jar
│ spring-instrument-5.0.2.RELEASE-sources.jar
│ spring-instrument-5.0.2.RELEASE.jar
│ spring-jcl-5.0.2.RELEASE-javadoc.jar
│ spring-jcl-5.0.2.RELEASE-sources.jar
│ spring-jcl-5.0.2.RELEASE.jar
│ spring-jdbc-5.0.2.RELEASE-javadoc.jar
│ spring-jdbc-5.0.2.RELEASE-sources.jar
│ spring-jdbc-5.0.2.RELEASE.jar
│ spring-jms-5.0.2.RELEASE-javadoc.jar
│ spring-jms-5.0.2.RELEASE-sources.jar
│ spring-jms-5.0.2.RELEASE.jar
│ spring-messaging-5.0.2.RELEASE-javadoc.jar
│ spring-messaging-5.0.2.RELEASE-sources.jar
│ spring-messaging-5.0.2.RELEASE.jar
│ spring-orm-5.0.2.RELEASE-javadoc.jar
│ spring-orm-5.0.2.RELEASE-sources.jar
│ spring-orm-5.0.2.RELEASE.jar
│ spring-oxm-5.0.2.RELEASE-javadoc.jar
│ spring-oxm-5.0.2.RELEASE-sources.jar
│ spring-oxm-5.0.2.RELEASE.jar
│ spring-test-5.0.2.RELEASE-javadoc.jar
│ spring-test-5.0.2.RELEASE-sources.jar
│ spring-test-5.0.2.RELEASE.jar
│ spring-tx-5.0.2.RELEASE-javadoc.jar
│ spring-tx-5.0.2.RELEASE-sources.jar
│ spring-tx-5.0.2.RELEASE.jar
│ spring-web-5.0.2.RELEASE-javadoc.jar
│ spring-web-5.0.2.RELEASE-sources.jar
│ spring-web-5.0.2.RELEASE.jar
│ spring-webflux-5.0.2.RELEASE-javadoc.jar
│ spring-webflux-5.0.2.RELEASE-sources.jar
│ spring-webflux-5.0.2.RELEASE.jar
│ spring-webmvc-5.0.2.RELEASE-javadoc.jar
│ spring-webmvc-5.0.2.RELEASE-sources.jar
│ spring-webmvc-5.0.2.RELEASE.jar
│ spring-websocket-5.0.2.RELEASE-javadoc.jar
│ spring-websocket-5.0.2.RELEASE-sources.jar
│ spring-websocket-5.0.2.RELEASE.jar
│
└───view
user.jsp
-> Создать контроллер.
package com.thp.project.vintud.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("/")
public String getUserList(Model model) {
model.addAttribute("message", "List of users");
return "user";
}
}
пользователь. 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>This is Spring MVC</title>
</head>
<body>
${message}
</body>
</html>
applicationContext . 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:context="http://www.springframework.org/schema/context"
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-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.thp.project.vintud" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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-mvc-demo</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/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>
Выход: