0

我已经配置了 hibernate 和 Spring,没有任何错误,并且我的项目正在部署,但是我尝试通过控制器“HomeController”加载我的 Home.jsp,但我得到“错误:404 找不到资源……”:


我尝试了 3 种不同的方法:

方法一(不是我理想的方法)。

a) 在欢迎列表 (web.xml) 中定义一个 index.htm

b) 使用@Controller @RequestMethod 通过注解将 HomeController 映射到“/index.htm”

c) HomeController 返回视图“Home” (WEB-INF/views/Home.jsp)


用于所有 3 种方法的通用项目结构:

在此处输入图像描述


Web.xml:

<web-app metadata-complete="true" 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>yourmarketnet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>yourmarketnet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout> 
            30 
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

Yourmarketnet-spring.xml 片段:

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

应用程序上下文.xml:

<!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.yourmarketnet.controller" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven/>
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <import resource="hibernate-context.xml" />

家庭控制器类:

package com.yourmarketnet.controller;
@Controller  
@RequestMapping("/index.htm") 
public class HomeController {  
    public String requestHandler()
    { 
        return "Home"; 
    } 
}   

运行应用程序的结果:

Http 状态 404

请求的资源 () 不可用。


方法二,(不是我理想的方法)

a) 创建 redirect.jsp ,并定义欢迎列表 (web.xml)

b) 使用@Controller @RequestMethod 通过注解将 HomeController 映射到“/HomeController”

c) 有 redirect.jsp “<% response.sendRedirect("/HomeController");%>” 到我的 HomeController

d) HomeController 返回视图“Home” (WEB-INF/views/Home.jsp)

Web.xml:

<web-app metadata-complete="true" 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>yourmarketnet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>yourmarketnet</servlet-name>
        <url-pattern>/</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>

Yourmarketnet-spring.xml 片段:

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

applicationContext.xml 片段:

<!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.yourmarketnet.controller" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven/>
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <import resource="hibernate-context.xml" />

家庭控制器类:

package com.yourmarketnet.controller;
@Controller  
@RequestMapping("/HomeController") 
public class HomeController {  
    public String requestHandler()
    { 
        return "Home"; 
    } 
}

运行应用程序的结果:

Http 状态 404 /HomeController

请求的资源 (/HomeController) 不可用。


方法 3,(如果上述任何方法都可以,但这是我真正想要的)

a) 从 web.xml 中删除所有欢迎文件标签

b) MapDispatcher 到“/”</p>

c) 使用 @Controller @RequestMethod' 通过注解将 HomeController 映射到“/HomeController”

d) HomeController 返回视图“Home” (WEB-INF/views/Home.jsp)

Web.xml:

<web-app metadata-complete="true" 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>yourmarketnet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>yourmarketnet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout> 
            30 
        </session-timeout>
    </session-config>
</web-app>

**Yourmarketnet-spring.xml 片段:**

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

applicationContext.xml 片段:

<!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.yourmarketnet.controller" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven/>
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <import resource="hibernate-context.xml" />

家庭控制器类:

package com.yourmarketnet.controller;
@Controller  
@RequestMapping("/") 
public class HomeController {

    public String requestHandler()
    { 
        return "Home"; 
    } 
}   

阿帕奇服务器日志:

警告:在名称为“yourmarketnet”的 DispatcherServlet 中找不到具有 URI [/yourmarketnet/] 的 HTTP 请求的映射

运行应用程序的结果:

Http 状态 404

请求的资源 () 不可用。

请为我提供正确的代码/标签,以便在没有 404 错误的情况下从 HomeController 服务我的 Home.jsp,谢谢。

4

1 回答 1

0

我说的是您问题中的第三种方法。

在您的 web.xml 中,将请求映射更改为/而不是*.htm. 其他一切,如果只是完美的话,我可以在这里看到。所以这应该适合你。

希望这对您有所帮助。

干杯。

于 2012-05-31T05:13:08.743 回答