gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:21 25 4
gpt4 key购买 nike

我是 Springs 的新手。我创建了一个程序来访问文件夹中的静态页面。当我点击按钮访问静态页面时。它抛出 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>

最终.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>

最佳答案

DispatcherServlet映射到特定模式(例如*.do),这样当您使用其路径访问静态文件时它就不会被命中

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

...

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

如果您想为页面提供动态内容,请确保请求以“.do”结尾。否则,只需确保它位于 WEB-INF 文件夹之外,以便向公众公开,从而可以通过其路径进行访问。

关于java - Spring MVC中如何访问静态页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33122177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com