0

我安装了 JBoss AS 7.1.1 服务器。我创建了我的项目战争文件并将其放在 [JBOSS_HOME]\standalone\deployments文件夹下。

我的应用程序是 Spring MVC,这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <display-name>My WebApp</display-name>

    <!-- Configurations for the root application context (parent context) -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/spring-jdbc.xml</param-value>
    </context-param>

    <!-- Controller -->
    <servlet>
        <servlet-name>myapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/myapp-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myapp</servlet-name>
        <url-pattern>/myapp/*</url-pattern>
    </servlet-mapping>



    <session-config>
        <session-timeout>400</session-timeout>
    </session-config>

    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/jsp/Error.jsp</location>
    </error-page>

    <welcome-file-list> 
    <welcome-file>/WEB-INF/jsp/welcome.jsp</welcome-file>
    </welcome-file-list>

</web-app>

myapp-servlet.xml

<context:annotation-config />
<context:component-scan base-package="com.mypackage.mvc" />

<bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>       


<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- the maximum file size in bytes = 10MB -->
    <property name="maxUploadSize" value="10485760" />
</bean>

在控制台日志中,myapp-servlet.xml 由 spring 加载,所有请求映射都是从我的控制器完成的。部分日志输出:

12:27:13,440 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-4) Mapped URL path [/myapp/welcome] onto handler 'myappController'
12:27:13,442 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-4) Mapped URL path [/myapp/welcome.*] onto handler 'myappController'
12:27:13,444 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-4) Mapped URL path [/myapp/welcome/] onto handler 'myappController'
12:27:13,527 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-4) FrameworkServlet 'myapp': initialization completed in 1028 ms
12:27:13,557 INFO  [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /myapp
12:27:13,565 INFO  [org.jboss.as] (MSC service thread 1-1) JBAS015951: Admin console listening on http://127.0.0.1:9990

我的控制器类很简单:

@Controller
@RequestMapping("/myapp")
public class MyappController {

    private static Logger log = Logger.getLogger(MyappController.class);

    // @formatter:off
    /**
     * Welcome to MyApp
     * 
     * 
     * @return welcome.jsp Page
     * 
     * 
     */
    // @formatter:on
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String welcome() {
        log.debug("welcome() called...");
        return "welcome";
    }

}

如果我访问http://localhost:8080/,那么我的welcome.jsp 会加载到网页中。

但问题是现在当我尝试使用以下网址访问时,我http://localhost:8080/myapp/welcome.jsp在网页中看到此错误:

HTTP Status 404 - /myapp/welcome.jsp

--------------------------------------------------------------------------------

type Status report

message /myapp/welcome.jsp

description The requested resource (/myapp/welcome.jsp) is not available.


--------------------------------------------------------------------------------

JBoss Web/7.0.13.Final

控制台日志:

11:46:05,871 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-8080-1) DispatcherServlet with name 'myapp' processing GET request for [/myapp/welcome]
11:46:05,873 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (http-localhost-127.0.0.1-8080-1) Looking up handler method for path /welcome
11:46:05,875 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (http-localhost-127.0.0.1-8080-1) Did not find handler method for [/welcome]
11:46:05,876 WARN  [org.springframework.web.servlet.PageNotFound] (http-localhost-127.0.0.1-8080-1) No mapping found for HTTP request with URI [/myapp/welcome] in DispatcherServlet with name 'myapp'
11:46:05,878 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-8080-1) Successfully completed request

这里有什么问题?

4

1 回答 1

0

您应该使用http://localhost:8080/myapp/welcome当前配置访问您的应用程序。您已将 jsp 文件放在 WEB-INF 中,这意味着它们不能直接访问,这是 Spring 框架中的标准做法(否则也是合理的默认设置。)

如果您想直接访问welcome.jsp,您需要将它移到WEB-INF 之外。但是,我不会那样做,请求应该始终通过 servlet,而不是直接指向 .jsps。

于 2013-09-22T09:54:07.830 回答