728x90
반응형
728x90
반응형

 

Spring Model, ModelMap, ModelAndView 차이점

 

Model, ModelMap Vs ModelAndView 차이점

  • 데이터만 저장한다 vs 데이터와 이동하고자 하는 View Page를 같이 저장한다

 

 

Model, ModelMap 공통점

  • model.addAttribute("변수명");
  • modelMap.addAttribute("변수명");
  • 둘 다 addAttribute를 사용함
  • Model or ModelMap에 데이터만 저장 후 View에서 사용목적

 

Model, ModelMap 차이점

  • Model - 인터페이스
  • ModelMap - 클래스

 

Java Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
 
@RequestMapping(value = "/test.do")
public String test(HttpServletRequest request, Model model, ModelMap modelMap){
        
    String modelStr = "Model Test";
    String modelMapStr = "ModelMap Test";
    
    model.addAttribute("modelVar", modelStr);
    model.addAttribute("modelMapVar", modelMapStr);
        
    return "temp/test";
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

JSP

1
2
3
4
5
<body>
    Model 저장한 값 : <input type="text" value="${modelVar }"/><br/>
    ModelMap 저장한 값 : <input type="text" value="${modelMapVar }"/>
</body>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

Web Page

 

 

ModelAndView

  • addObject를 통해 데이터를 저장
  • setViewName을 통해 이동하고자 하는 View를 저장
  • 메소드 안에서 ModelAndView mv = new ModelAndView(); 
  • return type ModelAndView 

 

Java Controller

1
2
3
4
5
6
7
8
9
10
11
@RequestMapping(value = "/test.do")
public ModelAndView test(HttpServletRequest request, ModelAndView mv){
        
    String modelAndViewStr = "ModelAndView Test";
    
    mv.addObject("modelAndViewVar", modelAndViewStr);
    mv.setViewName("temp/test");
        
    return mv;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

JSP

1
2
3
4
<body>
    ModelAndView 저장한 값 : <input type="text" value="${modelAndViewVar }"/><br/>
</body>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

Web Page

728x90
반응형
728x90
반응형

 

Ajax 동기방식으로 처리하기

 

async : false 추가하기

1
2
3
4
5
6
7
8
$.ajax({
    type : "POST",
    url : url,
    async : false,
    success : function(data) {
        console.log(data);
    }
});

 

해당 Ajax가 끝난 후에 다른 부분을 실행한다.

Default async : true

728x90
반응형

'Web > jQuery' 카테고리의 다른 글

[jQuery] CSS Control Example  (2) 2019.08.25
[jQuery] keyup Event Example  (0) 2019.08.25
[jQuery] Click Event Example  (0) 2019.07.14
[jQuery] AJAX 한글깨짐 해결 Example  (0) 2019.06.23
[jQuery] AJAX 배열전송(Array) Example  (0) 2019.06.22
728x90
반응형

 

스프링에서 세션 사용하기

 

 

1. 세션에 Data 저장하기

session.setAttribute("저장 하고자 하는 변수이름", 저장변수값);

Java Controller

1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping(value = "/test.do")
public ModelAndView test(HttpServletRequest request) throws Exception {
        
    HttpSession session = request.getSession();
    String name = "세션저장하기";
    session.setAttribute("ssVar", name);
 
    ModelAndView mv = new ModelAndView();
    mv.setViewName("/test/test");
 
    return mv;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

JSP

1
2
3
4
<body>
    <input type="text" value="${ssVar }"/>
</body>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

2. 세션에 저장된 Data 가져오기

session.getAttribute("저장한 변수 이름");

Java Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RequestMapping(value = "/test2.do")
public ModelAndView test2(HttpServletRequest request) throws Exception {
        
    ModelAndView mv = new ModelAndView();
        
    HttpSession session = request.getSession();
    String name = (String) session.getAttribute("ssVar");
        
    System.out.println("==============================");
    System.out.println("세션에 저장 되 있는 변수 : "+name);
    System.out.println("==============================");
        
    name = "세션값 변경";
    session.setAttribute("ssVar", name);
    mv.setViewName("/test/test");
        
    return mv;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

Java Console 확인

 

JSP

1
2
3
4
<body>
    <input type="text" value="${ssVar }"/>
</body>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 3. 세션 초기화 하기

session.invalidate();

728x90
반응형
728x90
반응형

 

jQuery 클릭 이벤트 예제

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 방법 1 */
$('#ID').on("click"function(){
    
});
 
/* 방법 2 */
$('#ID').bind("click"function(){
 
});
 
/* 방법 3 */
$('#ID').click(function(){
 
);
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
728x90
반응형

'Web > jQuery' 카테고리의 다른 글

[jQuery] keyup Event Example  (0) 2019.08.25
[jQuery] AJAX 동기 처리 Example  (0) 2019.07.21
[jQuery] AJAX 한글깨짐 해결 Example  (0) 2019.06.23
[jQuery] AJAX 배열전송(Array) Example  (0) 2019.06.22
[jQuery] AJAX Example  (0) 2019.06.22
728x90
반응형

 

JSP파일에서

  • JS 파일 Import 하기
  • CSS 파일 Import 하기
  • JSP 파일 Import 하기

 

JS 파일 Import 하기

1
2
3
4
5
js 파일 Import 방법 1
<script src="<c:url value='/js파일경로'/>" type="text/javascript"></script>
 
js 파일 Import 방법 2
<script src="/js파일경로" type="text/javascript"></script>
 

 

CSS 파일 Import 하기

1
2
3
4
5
Css 파일 Import 방법 1
<link type="text/css" rel="stylesheet" href="<c:url value='/css파일경로'/>" ></link>
Css 파일 Import 방법 2
<link type="text/css" rel="stylesheet" href="/css파일경로" ></link>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

JSP파일에서 다른 JSP 파일 Import 하기

1
2
3
다른 JSP 파일 Import 방법
<%@include file="/WEB-INF/jsp파일경로"%>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
728x90
반응형

'Web > JSP' 카테고리의 다른 글

[JSP] JSTL forEach Example  (0) 2019.07.07
[JSP] JSTL if, when, otherwise Example  (1) 2019.07.07
[JSP] JSTL Tag 사용 Error  (0) 2019.07.07
728x90
반응형

 

JSTL 문법 forEach 알아보기

0 ~ 5 까지 출력하기 (default step = 1)

1
2
3
4
5
6
<c:forEach var="index" begin="0" end="5">
    ${index }
</c:forEach>
 
실행 결과 : 0 1 2 3 4 5
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

0 ~ 5 까지 출력하기 (step = 2)

1
2
3
4
5
6
<c:forEach var="index" begin="0" end="5" step="2">
    ${index }
</c:forEach>
 
실행 결과 : 0 2 4
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

리스트 순차적으로 사용하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<c:set var="index" value="0,1,2" />
    <c:forEach items="${index }" var="temp">
        <p>인덱스 : ${temp }<p>
</c:forEach>
 
실행결과
-----------------------
인덱스 : 0
 
인덱스 : 1
 
인덱스 : 2
-----------------------
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
728x90
반응형

'Web > JSP' 카테고리의 다른 글

[JSP] js, css import example  (0) 2019.07.07
[JSP] JSTL if, when, otherwise Example  (1) 2019.07.07
[JSP] JSTL Tag 사용 Error  (0) 2019.07.07
728x90
반응형

 

Java의 If, Else 문법은 JSTL에서 whenotherwise를 사용한다.

1. IF문(단일) 사용하기

2. IF ELSE 사용하기

3. 다중 IF 사용하기

 

주의사항 

JSP파일 상단에 C taglib 추가하기

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

 

1. 단일 IF

1
2
3
4
<c:set var="index" value="0" />
<c:if test="${index eq 0 }">
    index 값 : ${index }
</c:if>
 

 

2. If Else문 (<c:choose> <c:otherwise>)

주의사항 - <c:when> <c:otherwise>(if else)문 사용할 경우 <c:choose></c:choose>로 감싸 주기

1
2
3
4
5
6
7
8
9
<c:set var="index" value="0" />
<c:choose>
    <c:when test="${index eq 1 }">
        index 값은 1입니다.
    </c:when>
    <c:otherwise>
        index 값 : ${index }
    </c:otherwise>
</c:choose>

 

3. 다중 If

eq(==) / ne(!=)

 

방법 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<c:set var="index" value="0" />
<c:set var="name" value="홍길동" />
        
<c:choose>
    <c:when test="${index eq 0 }">
        <c:if test="${name eq '홍길동' }">
            name 변수 값은 홍길동입니다.
        </c:if>
        <c:if test="${name eq '홍길동1' }">
            name 변수 값은 ${name }입니다.
        </c:if>
    </c:when>
    <c:otherwise>
        index 값 : ${index }
    </c:otherwise>
</c:choose>

 

방법 2

주의사항 - <c:when><c:otherwise> 앞에 <c:choose></c:choose>태그로 감싸주기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<c:set var="index" value="0" />
<c:set var="name" value="홍길동1" />
        
<c:choose>
    <c:when test="${index eq 0 }">
        <c:choose>
            <c:when test="${name eq '홍길동' }">
                name 변수 값은 홍길동입니다.
            </c:when>
            <c:when test="${name eq '홍길동1' }">
                name 변수 값은 홍길동1입니다.
            </c:when>
            <c:otherwise>
                name 변수 값은 ${name }입니다.
            </c:otherwise>
        </c:choose>
    </c:when>
    <c:otherwise>
        index 값 : ${index }
    </c:otherwise>
</c:choose>

 

 

728x90
반응형

'Web > JSP' 카테고리의 다른 글

[JSP] js, css import example  (0) 2019.07.07
[JSP] JSTL forEach Example  (0) 2019.07.07
[JSP] JSTL Tag 사용 Error  (0) 2019.07.07
728x90
반응형

 

Spring or JSP 파일에서 JSTL 태그 사용하는데 인식을 못할 때

 

JSP파일 안에 taglib를 추가해야 한다

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

 

 

 

 

 

728x90
반응형

'Web > JSP' 카테고리의 다른 글

[JSP] js, css import example  (0) 2019.07.07
[JSP] JSTL forEach Example  (0) 2019.07.07
[JSP] JSTL if, when, otherwise Example  (1) 2019.07.07

+ Recent posts