我的Spring应用程序不会打开另一个页面

时间:2013-02-06 22:50:00

标签: spring spring-mvc

我使用netbeans创建了一个Spring应用程序,我没有更改任何配置。在index.jsp中,我有一个指向另一个页面的链接,但它没有到达第二页并显示“请求的资源()不可用。”

在服务器控制台中显示以下警告

"WARNING: No mapping found for HTTP request with URI [/Myapp/emp.htm] in DispatcherServlet with name 'dispatcher'"

我的emp.jsp文件位于jsp文件夹中。

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <a href="emp.htm">emp</a>
    </body>
</html>

调度员的servlet

 <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

    <bean name="/emp.htm" class="controller.Employee"/> <<I changed this to "/Myapp/emp.htm" as well but does not work

</beans>

Web.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

Employee.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

public class Employee implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");

        return new ModelAndView("emp.jsp");
    }

}

3 个答案:

答案 0 :(得分:0)

更改<a href>以获取此类实际网址

  <a href="<c:url value="/emp"/>" />

还要确保Dispatcher servlet

中已映射web.xml
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet> 
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
 </servlet-mapping>

有关Spring MVC实现的更多信息,请访问here

答案 1 :(得分:0)

我已经通过添加

解决了这个问题

<prop key="emp.htm">empController</prop>

<bean name="empController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="emp" />

答案 2 :(得分:0)

@Daniel 是的,我可以看到emp.jsp是一个简单的消息jsp。问题是 MVC 模式是如何工作的(与Spring无关)。 MVC就是让调度员坐在所有请求前面并将请求路由到适当的资源(例如控制器,servlet等) 如果jsp中的消息是动态生成的,我认为请求应该通过控制器并转发到emp.jsp。如果emp.jsp的重点只是某种静态页面,你可以将emp.js放在 webapp / static / emp.jsp 下,并在你的spring servlet配置文件中指出/ static下的所有东西应该通过Spring ServletDispatcher

配置文件将如下所示

<mvc:resources mapping="/static/**" location="/static/" />

通过执行此操作,请求/static/emp.jsp将起作用,并且不会通过Spring DispatcherServlet。