Spring MVC请求的资源不可用
我有一个带有公共函数索引()的单个控制器类(MainController.class)的战争项目,该函数返回一个字符串,该字符串是我最后一次检查视图的名称正在转发 - 和一个名为“index.jsp”的jsp。这些视图位于/ main/webapp/WEB-INF/views/jsp /中。 MainController.java:Spring MVC请求的资源不可用
@Controller
public class MainController
{
@Autowired
private ApplicationContextProvider mObjApplicationContextProvider;
@Autowired
private ScheduleService mObjScheduleService;
protected ApplicationContext getApplicationContext()
{
return(getApplicationContextProvider().getApplicationContext());
}
protected ApplicationContextProvider getApplicationContextProvider()
{
return(mObjApplicationContextProvider);
}
protected Object getBean(final Class<?> clsBean)
{
return(getApplicationContext().getBean(clsBean));
}
protected Object getBean(final String sBeanName)
{
return(getApplicationContext().getBean(sBeanName));
}
protected ScheduleService getScheduleService()
{
return(mObjScheduleService);
}
@RequestMapping(value="/")
public String index()
{
return("index");
}
}
的index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello World!
</body>
</html>
我的XML配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="net.draconia.church" />
<bean class="net.draconia.ApplicationContextProvider" />
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql:sql9.freemysqlhosting.net/sql9158326" />
<property name="username" value="sql9158326" />
<property name="password" value="MqX7sbacgB" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
出于某种原因,我得到了我的浏览器中的错误,当我去“http://localhost:8080/Ushering/ “:404 - ”请求的资源不可用。“
我在日志中找不到任何东西(但可以发布,如有必要)告诉我什么文件或资源无法使用。它似乎能够找到控制器,因为它在一个日志文件中提到了控制器的请求映射。会是什么呢?
编辑:WAR文件按照在pom文件中定义的方式启动,也在web.xml中作为项目的名称,所以我会认为上下文将是启动以及任何在Spring应用程序端的事情会在/引发之后,所以如果我将请求映射设置为/ Hello,那么URL应该是localhost:8080/Ushering/Hello。
编辑:我的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" version="3.0">
<display-name>CrunchifySpringMVCTutorial</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Ushering</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Ushering-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Ushering</servlet-name>
<url-pattern>/s</url-pattern>
</servlet-mapping>
</web-app>
如果你看看你的MainController类只有一个请求处理方法,它返回“指数”。它被映射到“/”,这意味着当你键入localhost:8080它会给你索引文件作为回报。因此,不要打字本地主机:8080 /使用输入localhost:8080。它会工作。但是,如果您想要输入localhost:8080 /引导然后将控制器文件中的请求映射更改为以下内容。
@RequestMapping(值= “/迎来”)
public String index()
{
return("index");
}
它会回报你的索引文件。
我不明白这是怎么回事 - 战争是Ushering.war,当我将war文件复制到webapps时,它会在Tomcat的WebApps文件夹下创建一个上下文启动文件夹。如果我输入http:// localhost:8080,我会看到“如果你看到这个,你已经正式安装了Tomcat!恭喜!”页。 –
你能发布你的web.xml文件吗? – dennislloydjr
将其添加为编辑 –
我明白了!当我将web.xml -/s贴在底部附近的url-pattern中时,我注意到了一个错字。它应该是/且只有/。我删除了's',干净的编译战争:战争编辑它,它出现了,因为我期待:-D谢谢你在正确的方向戳我! –