如何在Spring MVC中访问静态页面?

时间:2015-10-14 09:55:06

标签: java spring spring-mvc

我是斯普林斯的新手。我创建了一个程序来访问该文件夹中的静态页面。 什么时候,我点击按钮访问静态页面。它抛出404错误。 有人可以让我知道它到底出了什么问题。下面是我的代码。                           webcontroller.java

import org.springframework.stereotype.Controller;
           import org.springframework.web.bind.annotation.RequestMapping;
           import org.springframework.web.bind.annotation.RequestMethod;
@Controller
        public class WebController {
        @RequestMapping(value="/index", method=RequestMethod.GET)
    public String index()
    {
        return "index";
    }
    @RequestMapping(value="/staticPage", method=RequestMethod.GET)
    public String redirect()
    {
        return "redirect:pages/final.html";
    }

}

的index.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">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Static Pages</title>
</head>
<body>
<h2>Spring Static Landing page</h2>
<p>Click below button to get a simple HTML page</p>
<form action="/StaticPageEg/staticPage" method="GET">
<table>
    <tr>
    <td>
    <input type="submit" value="Get HTML Page"/>
    </td>
    </tr>
</table>  
</form>
</body>
</html>

Final.html

<!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>Insert title here</title>
</head>
<body>
<h2>Simple HTML PAge</h2>
</body>
</html>

StaticPageEg-servlet.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-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.StaticPageEg"></context:component-scan>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

 <property name="prefix" value="/WEB-INF/jsp/" />
 <property name="suffix" value=".jsp"></property>
 </bean>

 <mvc:resources location="pages/" mapping="pages/**"/>
 <mvc:annotation-driven/>

 </beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name> Spring Static Pages</display-name>

<servlet>
<servlet-name>StaticPageEg</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>StaticPageEg</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

DispatcherServlet映射到某种模式(例如* .do),以便在使用其路径访问静态文件时不会被点击

<servlet-mapping>
<servlet-name>StaticPageEg</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

...

@RequestMapping(value="/index.do", method=RequestMethod.GET)

如果您要为包含动态内容的页面提供服务,请确保请求以&#34; .do&#34;结尾。否则,只需确保它在WEB-INF文件夹之外,以便它暴露给公众,因此可以通过其路径访问。

相关问题