好的,我知道这里有20个帖子有同样的问题,但是它们似乎都没有帮助我,所以这可能是重复的,但我已经查看了所有其他帖子,但没有一个解决了我的问题,所以必须有一些我做错了或者我没有从前面提到的问题的答案做正确的修改.
我正在尝试使用Spring创建一个小应用程序,我仍在尝试使用它,但我花了4天时间试图找出错误的原因,而我却做不到.每当我尝试从控制器返回jsp时,我仍然会收到HTTP状态404.通过Tomcat只有404状态,没有别的...
WebAppController:
@Controller
public class WebAppController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String login() {
System.out.println("You have entered the login maprequest");
return "test1";
}
}
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<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_2_5.xsd"
version="2.5">
<display-name>Hotel Application</display-name>
<servlet>
<servlet-name>WebApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)
webApp.xml:
<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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.iquestgroup" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
此配置在一个简单的maven项目中工作,只有上面提到的.问题是,完全相同的事情不适用于具有3个模块(持久性,服务和webapp)的maven项目.在webapp中,我复制了完全相同的东西,当我在服务器上运行时,我得到404 http状态......即使模块正在建立成功.
LE接受答案的第一部分是指那些以Spring开头的人所犯的常见servlet映射错误.我的问题与它无关,我在最初的答案后最终删除了它.为了不让读者感到困惑,接受答案的第一部分是指以下代码:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)