欢迎文件在春季不适用于 html 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24670327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Welcome file not working with html file in spring
提问by Sarika.S
I have given my welcome file in web.xml
But when running the application, it is showing 404 error on http://172.16.2.16:8080/sampletest/
我在 web.xml 中提供了我的欢迎文件但是在运行应用程序时,它显示 404 错误 http://172.16.2.16:8080/sampletest/
It is a spring application.
这是一个弹簧应用程序。
web.xml
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>sampletest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Spring MVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I am using eclipse luna, java 8, tomcat 8 and maven framework.
index.html file is directly under webapp folder and web.xml is under webapp/WEB-INF folder.
If I use index.jsp instead of index.html, it is working. Then welcome page will load using http://172.16.2.16:8080/sampletest/
我正在使用 eclipse luna、java 8、tomcat 8 和 maven 框架。index.html 文件直接位于 webapp 文件夹下,而 web.xml 位于 webapp/WEB-INF 文件夹下。
如果我使用 index.jsp 而不是 index.html,它就可以工作。然后欢迎页面将加载使用http://172.16.2.16:8080/sampletest/
The issue is only with welcome file. Otherwise spring configuration is working.
http://localhost:8080/sampletest/test/
will load the data from database.
问题仅与欢迎文件有关。否则弹簧配置正在工作。
http://localhost:8080/sampletest/test/
将从数据库加载数据。
Error log in console
控制台中的错误日志
......................
......................
Jul 10, 2014 12:38:42 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4963 ms
Jul 10, 2014 12:38:42 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/sampletest/] in DispatcherServlet with name 'dispatcher'
index.html
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
Dispatcher
调度员
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<context:component-scan base-package="com.sample.test" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<array>
<value>com.sample.test.domain</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.CharSet">UTF-8</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sampletest?autoConnect=true" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- HibernateTransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
<property name="flushModeName">
<value>FLUSH_AUTO</value>
</property>
</bean>
</beans>
Controller
控制器
package com.sample.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sample.test.dto.Response;
import com.sample.test.facade.AccelFlowFacade;
@Controller
public class SampleTestController {
@Autowired
@Qualifier("sampleTestFacade")
SampleTestFacade sampleTestFacade;
public SampleTestFacade getSampleTestFacade() {
return sampleTestFacade;
}
public void setSampleTestFacade(SampleTestFacade sampleTestFacade) {
this.sampleTestFacade= sampleTestFacade;
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody Response display() throws Exception {
sampleTestFacade.disaply();
Response res = new Response();
return res;
}
}
回答by Shinichi Kai
回答by Santhosh
You have mapped all your incoming requests to the dispatcher
here,
您已将所有传入请求映射到dispatcher
此处,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
So all your URL
requests for the application goes inside the dispatcher as '/' maps all incoming requests . check for the stacktraces in your application server log
因此,您URL
对应用程序的所有请求都会进入调度程序,因为“/”映射所有传入请求。检查应用程序服务器日志中的堆栈跟踪
update:
更新:
You get the below warning because there are no handler for the '/' pattern,
您收到以下警告,因为“/”模式没有处理程序,
WARNING: No mapping found for HTTP request with URI [/AccelFlow/] in DispatcherServlet with name 'dispatcher'
警告:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [/AccelFlow/] 的 HTTP 请求的映射
You can do either of below options ,
您可以执行以下任一选项,
- Map a url with '/' to the controller
- Add a specific URL pattern to the spring dispatcher such as
.htm or .do
as you wish
- 将带有“/”的 url 映射到控制器
- 将特定的 URL 模式添加到 spring 调度程序,
.htm or .do
如您所愿
Modify your web.xml,
修改你的web.xml,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
And in your controller,
在你的控制器中,
@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public @ResponseBody Response display() throws Exception {
accelFlowFacade.disaply();
Response res = new Response();
return res;
}
回答by Saifulcse01
At the startup by default all incoming requests are mapping to '/' pattern as you write in the web.xml:
默认情况下,在启动时,所有传入请求都映射到您在 web.xml 中写入的“/”模式:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
update:
更新:
Try to map a Controller method for the default view:
@RequestMapping(value = "/", method = GET) public String welcome() { return "index"; }
Add viewresolver to dispather-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
Remove welcome file from the web.xml as automatically spring will search for index page by default:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
尝试为默认视图映射 Controller 方法:
@RequestMapping(value = "/", method = GET) public String welcome() { return "index"; }
将 viewresolver 添加到 dispather-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
从 web.xml 中删除欢迎文件,因为默认情况下 spring 将自动搜索索引页面:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
回答by kp jangid
Welcome file can be access using following changes.
可以使用以下更改访问欢迎文件。
change 1. add resource path in dispatcher as following :
更改 1. 在调度程序中添加资源路径如下:
<mvc:resources mapping="/" location="/index.html" />
change 2. add controller handler like following :
更改 2. 添加控制器处理程序,如下所示:
@Controller
public class RestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index.html";
}
}
changes 3: index.html file should be in WebContent folder in project.
变化 3:index.html 文件应该在项目的 WebContent 文件夹中。
Note : If you are not able to add mvc bean in dispatcher servlet file then add
注意:如果您无法在调度程序 servlet 文件中添加 mvc bean,则添加
xmlns:mvc="http://www.springframework.org/schema/mvc
in dispatcher servlet config file.
在调度程序 servlet 配置文件中。
回答by Ali.Mojtehedy
using <mvc:resources mapping="/" location="/index.html" />
is goods for static pages , however changing it to
using <mvc:resources mapping="/" location="/index.html" />
is good for static pages ,但是将其更改为
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index.html";
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index.html";
}
is not good design as all model in other controllers may point back to index page, instead use
不是好的设计,因为其他控制器中的所有模型都可能指向索引页面,而是使用
<mvc:resources mapping="/" location="/redfresh.html" />
and make refresh page such
并制作刷新页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="refresh" content="0;URL='/index'" />
</head>
<body>
</body>
</html>
and point in controller to index such:
并在控制器中指向索引:
@Controller
public class indexPageController {
@RequestMapping(value = "/index", method = RequestMethod.GET, produces = "text/html")
public String index() {
return "index";
}
}