【问题标题】:Unable to load CSS and JS file after adding @REQUESTMAPPING annotation at class level在类级别添加 @REQUESTMAPPING 注释后无法加载 CSS 和 JS 文件
【发布时间】:2016-09-26 09:52:21
【问题描述】:

我在类级别为某些视图 (jsp) 添加了 @RequestMapping 注释。当我尝试使用正确的 URL 映射访问这些页面时,页面显示但无法正确呈现,并且无法加载 .js 和 .css 文件。在浏览器中,我得到以下错误。 GET 404(未找到) 在服务器日志中,我收到以下消息。 org.springframework.web.servlet.PageNotFound noHandlerFound 警告:在名称为“spring-dispatcher”的 DispatcherServlet 中找不到具有 URI [/ccb/invoice/webjars/adminlte/2.3.3/plugins/iCheck/icheck.min.js] 的 HTTP 请求的映射。 虽然 Servlet 配置文件中的静态资源映射非常好,并且对于那些在类级别没有 @requstmapping 注释的页面加载它,但不确定为什么事情会发生变化。

你能告诉我我哪里出错了。

@Controller @RequestMapping("/invoice") @SessionAttributes("ListOrderItems") public class InvoiceController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView adminPage() {
        System.out.println("Im in /Index** Mapping");
        String username;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        username = auth.getName(); //get logged in username
        username.toUpperCase();
        // Logic to build menue based on the Role of the user.
        HashMap hm = new HashMap();
        OrderItems od = new OrderItems();
        ModelAndView model = new ModelAndView();
        model.addObject("usr", username);
        //Set the object for Menue Links on the Page
        model.addObject("mnue", hm);
        model.setViewName("index");
        model.addObject("odi",od);
        return model;

    } }


Dispatcher Servlet configuration file: <mvc:resources mapping="/webjars/**" location="/webjars/" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven/>
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

【问题讨论】:

    标签: javascript css spring-mvc


    【解决方案1】:

    尝试在调度程序 servlet 中添加 xmlns

    xmlns="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    

    然后

    <resources mapping="/resources/**" location="/resources/" />
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2019-12-12
      • 2019-01-24
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多