我正在从一个多元化的课程(使用spring mvc构建Web应用程序)及其练习文件研究Spring MVC。通过使用原始项目,我发现我的项目无法在资源目录中加载.jsp文件。前缀和后缀已在bankAppContext.xml中设置,但是该项目永远无法加载正确的.jsp文件。
出于好奇,我在该项目下创建了一个新文件“ index.html”。事实证明,当我启动本地Tomcat服务器时,无论打算加载.jsp文件的前缀和后缀设置如何,它都将“ index.html”作为主页加载。如果我没有此“ index.html”,则主页返回“ 404未找到-源服务器未找到目标资源的当前表示或不愿意透露该资源的存在”。与其他端点(例如“ / new”)相同的404错误。
我的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