java组织。springframework。网状物servlet。PageNotFound NoHandler未找到URI为的HTTP请求的映射
我知道有一千个这样的问题,但我是瞎子,找不到错误。用于此设置的。jsp页面一切正常(令人惊讶地指向http://localhost:8082/spring/之后,http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
页面未加载)。我在这方面是新手,所以我可能误解了我在做什么/
当我切换到<property name="suffix" value=".html" />
(我有两个文件index.jsp和index.html)时,我不能从http://localhost:8082/spring/
或这个http://localhost:8082/spring/WEB-INF/spring/static/index.html
链接加载页面
mar 14, 2015 8:38:07 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring/WEB-INF/spring/static/index.html] in DispatcherServlet with name 'appServlet'
servlet上下文。xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/spring/static/" />
<property name="suffix" value=".html" />
</bean>
<context:component-scan base-package="net.codejava.spring" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/usersdb"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>
家庭控制器:
@Controller
public class HomeController {
@Autowired
private UserDAO userDao;
@RequestMapping(value="/")
public ModelAndView home() {
List<User> listUsers = userDao.list();
//ModelAndView model = new ModelAndView("home");
ModelAndView model = new ModelAndView("index");
model.addObject("userList", listUsers);
return model;
}
}
网络。xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
# 1 楼答案
首先,你的网络。xml不仅仅是放在文件中。例如,在Tomcat中,您的web。xml文件与Tomcat默认web合并。xml(参见Tomcat目录中的
conf/web.xml
)。在那里,您可以发现*.jsp
形式的URL被映射到JSP ServletJava Web应用程序以servlet的形式工作(将它们映射到Web.xml中的URL)。例如,您将
DispatchServlet
映射到/
的URL。 这个dispatch servlet“神奇地”与Spring一起工作,加载控制器,并在“内部”处理它们的映射。这就是为什么你使用@RequestMapping(value="/")
在控制器内部,您基本上可以命令将请求发送到名为
index
的视图,然后通过InternalResourceViewResolver
解析,形成/WEB-INF/spring/static/
+index
+.jsp
的路径,就像您在上下文的描述符文件中配置的那样基本上,这会发出一个内部分派请求(不完全是这样,但您现在可以这样想)——然后在第一个请求时使用完全相同的规则处理该请求——例如被
*.jsp
servlet捕获,然后进行处理。然而,像/WEB-INF/spring/static/index.html
这样的请求无法处理——找不到匹配的规则要回答您的问题,您可以在JSP文件中放入任何HTML,但使用任何JSP标记并不是必须的。 然而,如果你真的想在没有任何解析的情况下使用纯HTML文件——那么考虑使用静态资源——你已经在
<mvc:resources mapping="/resources/**" location="/resources/" />
中设置了它——这基本上将/resources/目录的内容公开给世界,而无需任何处理# 2 楼答案
要了解问题所在,有几件事需要理解
首先,您不能直接访问WEB-INF下的页面,这就是为什么当您直接尝试
http://localhost:8082/spring/WEB-INF/spring/static/index.jsp
时,不会呈现JSP的原因对于html文件来说,问题是当你点击控件时,一个视图名被构造并用于请求转发。您的视图名将为
/WEB-INF/spring/static/index.html
,它将搜索一个servlet,该servlet应使用此扩展来处理请求,但它将失败(如果是.jsp,则将由JSPServlet处理)。由于没有servlet能够处理该请求,默认servlet将启动并尝试找到一个控制器方法来处理该请求,并将失败,并显示消息no mapping found for /WEB-INF/spring/static/index.html
最好将控制器的视图结果配置为JSP。否则,下面的配置似乎适用于answer,或者尝试注册要由JSP servlet处理的html文件,如here