엄지월드

Spring 데이터 흐름 본문

java/Spring

Spring 데이터 흐름

킨글 2017. 7. 17. 21:06
반응형

항상 헷갈려서 정리 해 놔야겠다. 완죤 꿀팁!!


jsp -> controller

1. 파라메터로 받기

jsp

<a href="<c:url value="/notices?page=${page }" /> ">파라메터로 받기</a>


controller

@RequestMapping(value="/notice/view/{no}, method = RequestMethod.GET

public String ControllerName(@PathVariable int no){

return "index";

}


2. jsp내의 input name으로 받기 예시

jsp

<input type="text" name="page" >


controller

@RequestMapping(value = "/notice/write", method = RequestMethod.POST

public String ControllerName(@RequestParam(value = "page") int page ){

return "index";

}


3. form 값으로 vo통해 받기

jsp

<input type="text" name="title">

<input type="text" name="content">


controller

@RequestMapping(value="/notice/write", method = RequestMethod.POST)

public String ControllerName(@ModelAttribute Notice notice){

notice.getTitle(); // vo를 통해 title을 가져온다.

notice.getContent(); // vo를 통해 content를 가져온다.

}


vo

public class Notice{

private String title; private String content;

public String getTitle(){ return title; }

public String getContent(){ return content; }

}


jsp(ajax) -> controller

jsp

$.ajax({  

type: 'POST',  

url: '${pageContext.request.contextPath}/signs/approval',  

data: {"Doc":Doc},  

      success:function(data){   

alert("성공");

window.close();

      },

error:function(jqXHR, testStatus, errorThrown){

alert("에러 발생~~");

}

});  

controller

String Doc = request.getParameter("Doc");


jsp -> controller -> json

1. ajax post 방식으로 요청 후 반환 이중 ArrayList

ajax

<input type="button" id="searchButton1" value="눌러보쇼">

<script>

$("#searchButton1").click(function(){

var values = []; //ArrayList 값을 받을 변수를 선언

$.post(

"showJsonData1", // 실행 url

    function(jsonObject) {

console.log(jsonObject); // 로그 찍기

    var innerHTML =""; 

values = jsonObject;

alert(values);

// innerHTML +="<h1>"+values+"</h1>";

innerHTML +="<table border='1' cellspacing='0'><tr><td>번호</td><td>제목</td><td>arr</td>";

$.each(values, function(index, value){ // 출력

// alert(index);

innerHTML +="<tr>";

innerHTML +="<td><input type='text' "+value+" ></td>";

innerHTML +="</tr>";

});

innerHTML +="</table>";

$('#come1').append(innerHTML); // 화면에 표시

        }

    );

});

</script>


controller

@RequestMapping(value="showJsonData1", method=RequestMethod.POST)

@ResponseBody

public Object get(@RequestParam Map<String,Object> map) {

// 이중 ArrayList 방식

ArrayList<Object> mChildList = new ArrayList<Object>(); 

ArrayList<Object> subChild = null;

ArrayList<ArrayList<Object>> mGroupList = new ArrayList<ArrayList<Object>>();

 

    subChild = new ArrayList<Object>();

    subChild.add("value='test1'");

    subChild.add("font-color='red'");

    mChildList.add(subChild);

   

    subChild = new ArrayList<Object>();

    subChild.add("value='test2'");

    subChild.add("font-color='blue'");

    mChildList.add(subChild);

    

    subChild = new ArrayList<Object>();

    subChild.add("value='test3'");

    subChild.add("font-color='yellow'");

    mChildList.add(subChild); // 마지막 값

         

    mGroupList.add(mChildList);

   

    System.out.println(mChildList);

    return mChildList;

}


2. ajax post 방식으로 요청 _ ArrayList<Map>

ajax

<input type="button" id="searchButton" value="눌러보쇼">

<script>

$("#searchButton").click(function(){

var values = []; //ArrayList 값을 받을 변수를 선언

$.post(

"showJsonData", // 실행 url

    function(jsonObject) {

console.log(jsonObject); // 로그 찍기

    var innerHTML =""; 

alert("실행됫슈");

values = jsonObject.result_list;

innerHTML +="<table border='1' cellspacing='0'><tr><td>번호</td><td>제목</td><td>arr</td>";

$.each(values, function( index, value){ // 출력

innerHTML +="<tr>";

innerHTML +="<td><input type='text' value='"+value.idx+"'></td>";

innerHTML +="<td><input type='text' value='"+value.title+"'></td>";

innerHTML +="<td><input type='text' value='"+value.arr+"'></td>";

innerHTML +="</tr>";

});

innerHTML +="</table>";

$('#come').append(innerHTML); // 화면에 표시

        }

    );

});

</script>


controller

@RequestMapping(value="showJsonData", method=RequestMethod.POST)

@ResponseBody

public Object getget(@RequestParam Map<String,Object> map) {

// ArrayList<Map> 방식

Map<String, Object> jsonSubObject = null;

Map<String, Object> jsonObject = new HashMap<String, Object>();

ArrayList<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();

      int arr[] = null;

jsonSubObject = new HashMap<String, Object>();

jsonSubObject.put("idx", 5);

jsonSubObject.put("title", "제목입니다");

arr = new int[3]; // 사용할 때 마다 객체를 할당해주지 않으면 밑에 배열에서도 같은 값이 들어감

arr[0] =1;

arr[1] =2;

arr[2] =3;

      jsonSubObject.put("arr", arr);

      jsonList.add(jsonSubObject); 

   

      jsonSubObject = new HashMap<String, Object>();

      jsonSubObject.put("idx", 2);

      jsonSubObject.put("title", "두번째제목입니다");

      arr = new int[3];

      arr[0] = 4;

      arr[1] = 5;

      arr[2] = 6;

      jsonSubObject.put("arr", arr);

      jsonList.add(jsonSubObject);

   

      jsonObject.put("result_list", jsonList);

   

      System.out.println(jsonObject);   

      return jsonObject;

}


controller -> jsp

controller

model.addAttribute("keyToJsp",key);


jsp

${keyToJsp}


controller -> js


controller

int a = 3;

model.addAttribute("test1",a);


js

<script>

var jsA = '${test1}';

</script>

controller -> mapper

controller

int no = 3;

boardMapper.insertFiles(no);


mapper

@Insert("select from files where no = #{no} ")

public void selectFiles(@Param("no") int no);




기타 자주 쓰는 것

이전 페이지 :

<button onclick="javascript:history.back();" class="btn btn-lg btn-primary ">이전</button>

confirm 창 띄우기 : 

<a href="<c:url value="/notice" />" class="btn btn-lg btn-primary"onclick="if(!confirm('삭제 하시겠습니까?')){return false;}">삭제</a> 



'java > Spring' 카테고리의 다른 글

session에 값 넣어 사용하기  (0) 2017.08.20
Spring multi form ajax  (0) 2017.08.16
ajax를 이용한 로그인 폼 체크  (0) 2017.07.02
@Valid 폼 검증  (0) 2017.06.28
서버 구축 방법 - 2  (0) 2017.05.13
Comments