我在web.xml中提供了我的欢迎文件
但是在运行应用程序时,它在http://172.16.2.16:8080/sampletest/
这是春季申请。
的web.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>
我使用的是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/
问题仅在于欢迎文件。否则弹簧配置正常。
http://localhost:8080/sampletest/test/
将从数据库加载数据。
控制台中的错误日志
......................
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
分派器
<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>
控制器
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;
}
}
答案 0 :(得分:14)
尝试在dispatcher-servlet.xml中添加<mvc:default-servlet-handler/>
。
有关详细信息,请参阅here。
答案 1 :(得分:5)
您已将所有传入的请求映射到此处的dispatcher
,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
因此,您对该应用程序的所有URL
请求都会在调度程序内部进行,因为&#39; /&#39;映射所有传入的请求。检查应用程序服务器日志中的堆栈跟踪
<强>更新强>
您收到以下警告,因为&#39; /&#39;没有处理程序图案,
警告:找不到带有URI [/ AccelFlow /]的HTTP请求的映射 具有名称&#39;调度程序&#39;
的DispatcherServlet
您可以执行以下任一选项,
.htm or .do
修改 web.xml ,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
在你的控制器中,
@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public @ResponseBody Response display() throws Exception {
accelFlowFacade.disaply();
Response res = new Response();
return res;
}
答案 2 :(得分:3)
默认启动时,所有传入的请求都会在您在web.xml中写入时映射到'/'模式:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<强>更新强>
尝试为默认视图映射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>
答案 3 :(得分:0)
可以使用以下更改访问欢迎文件。
更改1.在调度程序中添加资源路径如下:
<mvc:resources mapping="/" location="/index.html" />
更改2.添加控制器处理程序,如下所示:
@Controller
public class RestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index.html";
}
}
更改3:index.html文件应该在项目的WebContent文件夹中。
注意:如果您无法在调度程序servlet文件中添加mvc bean,请添加
xmlns:mvc="http://www.springframework.org/schema/mvc
在调度程序servlet配置文件中。
答案 4 :(得分:0)
使用<mvc:resources mapping="/" location="/index.html" />
是静态页面的商品,但将其更改为
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index.html";
}
不是好设计,因为其他控制器中的所有模型都可能指向索引页面,而是使用
<mvc:resources mapping="/" location="/redfresh.html" />
并刷新页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="refresh" content="0;URL='/index'" />
</head>
<body>
</body>
</html>
并指向控制器索引这样的:
@Controller
public class indexPageController {
@RequestMapping(value = "/index", method = RequestMethod.GET, produces = "text/html")
public String index() {
return "index";
}
}
答案 5 :(得分:0)
只需将index.html添加到webContent文件夹。因为欢迎文件是在该文件夹本身中搜索的。