복붙노트

[SPRING] Spring MVC로 JSP에 js와 CSS를 포함시키는 법

SPRING

Spring MVC로 JSP에 js와 CSS를 포함시키는 법

내 jsp에 js 및 css 파일을 포함하고 싶지만 그렇게 할 수는 없습니다. 스프링 MVC 개념이 생소합니다. 오랜 시간 동안, 나는이 같은 주제에 대해 연구 해왔다. 색인 페이지는 다음과 같습니다.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js">

</script>

<style type="text/css">
body {
    background-image: url("LoginPageBackgroundImage.jpg");
}
</style>
</head>
<body >
    <h6>Please login in google Chrome</h6>
    <h1 align="center">Welcome to my Twitter Clone</h1>
    <div class="m" style="margin-left: 401px;   margin-top: 70px;">
        <form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);">
        <fieldset>
                <legend align="center">Login</legend>
                    <div class="a">
                        <div class="l">User Name</div>
                        <div class="r">
                            <INPUT type="text" name="userName">
                        </div>
                    </div>

                    <div class="a">
                        <div class="l">Password</div>
                        <div class="r">
                            <INPUT type="password" name="password">
                        </div>
                    </div>
                    <div class="a">
                        <div class="r">
                            <INPUT class="button" type="submit" name="submit"
                                value="Login">
                        </div>
                    </div>
                    <i align="center" style="margin-left: 183px;">New User?  <a href="signup.html"><u>Signup</u></a></i>
            </fieldset>
    </form>
    </div>
</body> 
</html>

그리고 내 spring-dispatcher-servlet.xml은 이것과 같습니다.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd">

        <context:component-scan base-package="com.csc.student" />
        <mvc:annotation-driven/>
        <!--<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->   
        <!-- <bean name="/welcome.html" class ="csc.csc.helloController.HelloController" /> -->
    <bean id="viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name ="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

내 컨트롤러는 이와 같습니다.

package com.csc.student;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class StudentInfoController {

        @RequestMapping(value = "/indexPage.html", method = RequestMethod.GET)
        public ModelAndView getAdmissionFrom() {
            ModelAndView model = new ModelAndView("indexPage");
            return model;
        }
    }

이걸 도와 줄 수 있습니까? 나는 열심히 고투하고 있지만 어떤 해결책도 얻지 못하고있다. 내 js 및 CSS 파일을 WEB-INF 폴더에 보관했습니다.

해결법

  1. ==============================

    1.먼저 다음과 같이 dispatcher-servlet 파일에서 자원을 선언해야합니다.

    먼저 다음과 같이 dispatcher-servlet 파일에서 자원을 선언해야합니다.

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

    url mapping / resources / **를 사용하는 요청은 / resources / folder /를 직접 찾습니다.

    이제 jsp 파일에서 다음과 같은 CSS 파일을 포함시켜야합니다.

    <link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
    

    마찬가지로 js 파일을 포함 할 수 있습니다.

    희망이 당신의 문제를 해결합니다.

  2. ==============================

    2.style.css를 WEB-INF 폴더가 아닌 webapp / css 폴더에 직접 넣으십시오.

    style.css를 WEB-INF 폴더가 아닌 webapp / css 폴더에 직접 넣으십시오.

    그런 다음 spring-dispatcher-servlet.xml에 다음 코드를 추가합니다.

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

    JSP 페이지에 다음 코드를 추가하십시오.

    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    

    나는 그것이 효과가 있기를 바랍니다.

  3. ==============================

    3.스프링 mvc가 아닌 스프링 만 사용하는 상황에서 다음 접근 방식을 취하십시오.

    스프링 mvc가 아닌 스프링 만 사용하는 상황에서 다음 접근 방식을 취하십시오.

    서블릿 디스패처에 다음을 추가합니다.

    <mvc:annotation-driven />               
    <mvc:resources mapping="/css/**" location="/WEB-INF/assets/css/"/>
    <mvc:resources mapping="/js/**" location="/WEB-INF/assets/js/"/>
    

    당신은 / css 스타일 시트 위치에 대해 알 수 있듯이 spring mvc에 필요한 폴더 구조가없는 경우 / resources 폴더에있을 필요가 없습니다. 스프링 응용 프로그램의 경우처럼 그렇습니다. 자바 스크립트 파일, 글꼴에 적용됩니다 if 너는 그들을 필요로한다.

    그런 다음 필요에 따라 리소스에 액세스 할 수 있습니다.

    <link rel="stylesheet" href="css/vendor/swiper.min.css" type="text/css" />
    <link rel="stylesheet" href="css/styles.css" type="text/css" />
    

    나는 대부분의 예제가 봄 mvc와 같이 유용하다는 것을 누군가가 알게 될 것이라고 확신한다.

  4. ==============================

    4.WEB-INF 폴더 아래의 항목에는 직접 액세스 할 수 없습니다. 브라우저가 CSS 파일을 요청하면 WEB-INF 폴더에서 볼 수 없습니다.

    WEB-INF 폴더 아래의 항목에는 직접 액세스 할 수 없습니다. 브라우저가 CSS 파일을 요청하면 WEB-INF 폴더에서 볼 수 없습니다.

    WebContent 아래에 파일 css / css 폴더를 넣어보십시오.

    Dispatcher 서블릿에 다음을 추가하여 액세스 권한을 부여하십시오.

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

    당신의 js 파일과 유사합니다. 여기에 좋은 예가있다.

  5. ==============================

    5.css / js 파일을 src / main / webapp / resources 폴더에 넣으십시오. WEB-INF 또는 src / main / resources에 두지 마십시오.

    css / js 파일을 src / main / webapp / resources 폴더에 넣으십시오. WEB-INF 또는 src / main / resources에 두지 마십시오.

    그런 다음이 줄을 spring-dispatcher-servlet.xml에 추가합니다.

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

    jsp 페이지에 css / js 파일 포함

    <link href="<c:url value="/resources/style.css" />" rel="stylesheet">
    

    jsp에서 taglib을 선언하는 것을 잊지 마십시오.

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
  6. ==============================

    6.Dispatcher 서블릿 파일에서 자원을 선언해야합니다. 다음은 두 가지 선언입니다.

    Dispatcher 서블릿 파일에서 자원을 선언해야합니다. 다음은 두 가지 선언입니다.

    <mvc:annotation-driven />
    <mvc:resources location="/resources/" mapping="/resources/**" />
    
  7. from https://stackoverflow.com/questions/26276603/how-to-include-js-and-css-in-jsp-with-spring-mvc by cc-by-sa and MIT license