0

我有以下弹簧控制器类。

@Controller
public class LayoutController {

    @RequestMapping(value = "/layout", method = RequestMethod.GET)
    public String ShowLayout(){
        return "redirect:/views/layout.html";
    }
}

这是我的通用配置 xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

        <!--  COMMON CONFIGURATIONS -->
        <mvc:annotation-driven/>
        <tx:annotation-driven/> 

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

        <!-- Annotations based Configuration -->
        <context:annotation-config />

        <mvc:resources mapping="/*" location="/WEB-INF/views/" />

        <!-- Components Auto-Detection (Backend) -->
        <context:component-scan base-package="com.jkcs.touchpos" use-default-filters="false" >
            <!-- Types annotated by Spring Managed, Controller and Transactional, or by an annotation that itself is 
            annotated by SpringManaged, Controller, Transactional -->
            <context:include-filter type="annotation" expression="com.jkcs.touchpos.platform.annotations.SpringManaged"/>
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.transaction.annotation.Transactional"/>
        </context:component-scan>

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

</beans>

这是我的 mvc-config xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

这是我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TouchPOS</display-name>
  <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>

最后是调度程序 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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--  SPECIFIC CONFIGURATIONS -->
    <import resource="springConfigurations/common-config.xml"/>
    <import resource="springConfigurations/mvc-config.xml"/>

</beans>

当我在码头服务器上运行此 Web 应用程序时,我使用以下 URL 来获取布局。但是服务器会抛出 404 错误。请帮我找出我在做的错误的事情?

http://localhost:8080/TouchPOSApplicatio/layout

更新:网页元素显示正确但没有 CSS。控制台打印了以下错误。

Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/style.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/jquery.custom-scrollbar.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery-1.9.1.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.colorbox.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.touchSwipe.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/functions.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/dragscrollable.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery-ui.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/colorbox.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.colorbox.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/functions.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.touchSwipe.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/dragscrollable.js] in DispatcherServlet with name 'dispatcher'
4

2 回答 2

1

我认为TouchPOSApplicatio这只是问题中的错字?我很难在这里理解你的模式。您已将静态资源映射到/WEB-INF/views,这是无法从浏览器访问的,所以我认为这不会起作用。尝试将其放入常规的 webapp 文件夹中,例如static(与 位于同一级别WEB-INF)。然后将您的配置更改为:

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

然后您可以通过键入来验证它是否可以访问http://localhost:8080/TouchPOSApplication/static/layout.html。如果这有效,那么您已经正确配置了静态资源。

编辑

如果您也有 css 资源,请确保将包含这些资源的文件夹也添加为 mvc:resource。

于 2013-04-19T08:31:33.263 回答
0

你的控制器被映射到@RequestMapping(value = "/layout", method = RequestMethod.GET),你的 servlet 映射是<url-pattern>/</url-pattern>,所以 URL 应该是http://localhost:8080/layout

此外,正如 NilsH 已经说过的,您的控制器重定向到浏览器无法直接看到的文件。它应该作为静态文件公开。

于 2013-04-19T08:45:56.137 回答