这可能是一个简单的,但我想我错过了一些东西.问题归结为:我正在尝试使用HelloController来显示"/WEB-INF/hello.jsp".不幸的是,我在尝试访问http://example.com/app/hello时获得了404
这是代码.可能很容易解决.
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>app</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/" p:suffix=".jsp" />
</beans>
Run Code Online (Sandbox Code Playgroud)
HelloController.java:
@Controller
public class HelloController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
return mv;
}
}
Run Code Online (Sandbox Code Playgroud)
为hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<p>Hello</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
更新:每个请求添加了错误消息.
错误404 - 从RFC 2068超文本传输协议找不到 - HTTP/1.1:10.4.5 404未找到
服务器未找到与Request-URI匹配的任何内容.没有说明该病症是暂时的还是永久性的.
如果服务器不希望将此信息提供给客户端,则可以使用状态代码403(禁止).如果服务器通过一些内部可配置的机制知道旧资源永久不可用且没有转发地址,则应该使用410(Gone)状态代码.
这个问题(中web.xml
):
<url-pattern>/*</url-pattern>
Run Code Online (Sandbox Code Playgroud)
这会将所有请求重定向到Spring servlet,包括从控制器到JSP的请求.从本质上讲,控制流程将从控制器循环回到Spring.您需要缩小范围,以便JSP的请求直接转到底层容器,而不是Spring.
尝试将其更改为
<url-pattern>/app*</url-pattern>
Run Code Online (Sandbox Code Playgroud)
然后再试一次.您可能需要使用前导和尾随板条来使其工作(例如<url-pattern>/app*</url-pattern>
或@RequestMapping("hello")
等)
归档时间: |
|
查看次数: |
19868 次 |
最近记录: |