Я изучаю Spring MVC из курса множественного взгляда (создание веб-приложения с использованием Spring mvc) с файлами упражнений.Используя оригинальный проект, я обнаружил, что мой проект не может загрузить файлы .jsp в каталог ресурсов.Префикс и суффикс были настроены в bankAppContext.xml, но проект никогда не сможет загрузить правильный файл .jsp.
Для любопытства я создал новый файл "index.html" прямо под этим проектом.Оказалось, что когда я запустил свой локальный сервер Tomcat, он загрузил этот «index.html» в качестве домашней страницы независимо от настроек префикса и суффикса, как я намеревался загрузить файлы .jsp.Если у меня нет этого «index.html», домашняя страница возвращает «404 Not Found - исходный сервер не нашел текущего представления для целевого ресурса или не хочет раскрыть, что он существует».Та же ошибка 404 для других конечных точек, таких как "/new".
Моя версия Intellij Ultimate: 2019.1.2, Java 1.8.0-191, Tomcat 7.0.94.Я также прилагаю свои скриншоты и коды.
AccountController.java
package com.ps.springmvc.psbankapp.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ps.springmvc.psbankapp.model.Account;
@Controller
public class AccountController {
@RequestMapping("/")
public String showHomePage() {
return "index";
}
@RequestMapping("/new")
public String newAccount() {
return "newAccount";
}
@RequestMapping("/showAccount")
public String showAccount() {
return "showAccount";
}
@RequestMapping(value="/saveAccount",method=RequestMethod.POST)
public String saveAccount(Model model,HttpServletRequest request) {
String acNo = request.getParameter("accountNo");
String customerName = request.getParameter("accountHolderName");
String balance = request.getParameter("balance");
/*model.addAttribute("accountNumber", acNo);
model.addAttribute("accountHolderName", customerName);
model.addAttribute("balance", balance); */
Account account = new Account();
account.setAccountNo(Integer.parseInt(acNo));
account.setAccountHolderName(customerName);
account.setBalance(Integer.parseInt(balance));
model.addAttribute("account",account);
return "showAccount";
}
}
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>PSBankApp</display-name>
<servlet>
<servlet-name>SpringController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bankAppContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
bankAppContext.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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.ps.springmvc.psbankapp"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/Views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Вот мои журналы запуска, которые запустили "index.html" как мой домстр.
C:\apache-tomcat-7.0.94\bin\catalina.bat run
[2019-06-05 08:02:16,201] Artifact PSBankApp: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE: "C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp"
Using CATALINA_HOME: "C:\apache-tomcat-7.0.94"
Using CATALINA_TMPDIR: "C:\apache-tomcat-7.0.94\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_191"
Using CLASSPATH: "C:\apache-tomcat-7.0.94\bin\bootstrap.jar;C:\apache-tomcat-7.0.94\bin\tomcat-juli.jar"
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.94
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Apr 10 2019 16:56:40 UTC
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.94.0
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_191\jre
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_191-b12
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\apache-tomcat-7.0.94
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp\conf\logging.properties
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote=
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.port=1099
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.ssl=false
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp\jmxremote.password
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp\jmxremote.access
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.rmi.server.hostname=127.0.0.1
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dignore.endorsed.dirs=
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\xtynd\.IntelliJIdea2019.1\system\tomcat\Unnamed_PSBankApp
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-7.0.94
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=C:\apache-tomcat-7.0.94\temp
Jun 05, 2019 8:02:17 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_191\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files\Microsoft MPI\Bin\;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Enterprise Vault\EVClient\x64\;%JAVA_HOME%\bin;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Python36\Scripts\;C:\Python36\;C:\Users\xtynd\AppData\Local\Microsoft\WindowsApps;C:\Users\xtynd\AppData\Local\GitHubDesktop\bin;C:\apache-maven-3.6.0\bin;%MAVEN_HOME%\bin;%SCALA_HOME%\bin;%CYGWIN_HOME%\bin;C:\mongodb-win32-x86_64-2008plus-ssl-4.0.6\bin;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;%CATALINA_HOME%\bin;C:\instantclient_11_2;C:\Python37-32\Scripts\;C:\Python37-32\;C:\Users\xtynd\AppData\Local\GitHubDesktop\bin;C:\Program Files\Fiddler;C:\tools\Cmder;;.
Jun 05, 2019 8:02:17 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 05, 2019 8:02:17 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 547 ms
Jun 05, 2019 8:02:17 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 05, 2019 8:02:17 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.94
Jun 05, 2019 8:02:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jun 05, 2019 8:02:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 05, 2019 8:02:17 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 52 ms
Connected to server
[2019-06-05 08:02:18,316] Artifact PSBankApp: Artifact is being deployed, please wait...
[2019-06-05 08:02:18,645] Artifact PSBankApp: Artifact is deployed successfully
[2019-06-05 08:02:18,645] Artifact PSBankApp: Deploy took 329 milliseconds
Jun 05, 2019 8:02:27 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.94\webapps\manager
Jun 05, 2019 8:02:27 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.94\webapps\manager has finished in 91 ms