有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringMVC无法获取静态资源

我正在开发一个Java应用程序,我正在使用Spring MVC。我是个新手,我正在尝试在我的主页上加载一些静态内容,比如css文件和图像。当我尝试加载其中一些文件时,会出现405错误(get方法不允许)

我的项目结构(用于web部件)是

-WebContent
--MetaInf
--resources
--views
--WEB_INF
---web.xml
---springapp-dispatcher.xml

这是我的网站。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"
        metadata-complete="true">

        <servlet>
            <servlet-name>springapp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>springapp</servlet-name>

            <url-pattern>/</url-pattern>
        </servlet-mapping>

</web-app>

我的springapp调度员。xml(不含开头部分)

<mvc:annotation-driven/>

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

    <context:component-scan base-package="polibeacon.web"></context:component-scan>

    <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

我只有一个控制器,名为HomeController。爪哇

@Controller
public class HomeController {
    @RequestMapping( value = "/", method = RequestMethod.GET )
    public String index(Model model) {
        model.addAttribute(new User());
        return "index";
    }

    @RequestMapping(value="/registration", method = RequestMethod.GET)
    public String registration(Model model) {
        model.addAttribute(new User());
        return "registration";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String registerUser(@Valid User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
              return "registration";
        }
        // save user
        return "index";
    }
}

这是索引。jsp页面

<!-- CSS -->
<link href="<c:url value="/resources/bootstrap/css/bootstrap.min.css" />" rel = "stylesheet">
<link href="<c:url value="/resources/css/style.css" />" rel = "stylesheet">

<!-- Scripts -->

</head>
<body>

    <div class = "container">
        <div class = "row">
            <div class = "col-md-3"></div>
            <div class = "col-md-6">
                <div class = "jumbotron" style = "text-align: center;">
                    <img src = "images/logo.png" style = "display: block; margin-left: auto; margin-right: auto;">
                    <h2>Header</h2>

                    <form:form method="post" modelAttribute="user">
                        <form:input path="username" placeholder="Username"/>
                        <form:password path="password" placeholder="Password"/>

                    </form:form>
                </div>
            </div>
        </div>
        <a href="<c:url value="registration"/>">Sign Up</a>
    </div>


</body>
</html>

怎么了


共 (0) 个答案