JSONObject obj = new JSONObject();

obj.put("id", result.getId());

obj.put("theme", result.getTheme());

obj.put("title", result.getTitle());

obj.put("address", result.getAddress());

obj.put("memo", result.getMemo());

obj.put("imgpath", result.getImgpath());


{"address":"서울 종로구 우정국로2길 21",

 "imgpath":"d:\\mapstarImg\\testId_20171229075929343.jpg",

 "memo":"눈치안보임",

 "theme":"스터디까페",

 "id":"testId",

 "title":"마이크임팩트스퀘어"}


이런식으로 {"키:벨류", "키":"벨류"} 로 담깁니다. 


JSONArray arr = new JSONArray();

arr.add(obj); 


[{"address":"서울 종로구 우정국로2길 21",

 "imgpath":"d:\\mapstarImg\\testId_20171229075929343.jpg",

 "memo":"눈치안보임",

 "theme":"스터디까페",

 "id":"testId",

 "title":"마이크임팩트스퀘어"}]


담아논 JSONObject를 JSONArray 안에 넣어줍니다. 

[ {"키:벨류", "키":"벨류"} ]  

//Ajax JSON response

response.setContentType("text/html; charset=utf-8"); 

PrintWriter writer = response.getWriter();

writer.print(arr.toString());

writer.flush();

writer.close()


response로 보냅니다.


Ajax에서 받아 JSON.parse해서 받습니다.
entry.키 값으로 벨류를 가져와서 각자 활용 합니다.

success: function (datas) {
var dataResult = JSON.parse(datas);
$.each(dataResult, function(index, entry) {
alert("entry::"+entry.id);
alert("entry::"+entry.title);
alert("entry::"+entry.address);
alert("entry::"+entry.theme);
alert("entry::"+entry.memo);
alert("entry::"+entry.imgpath);
});
},




Not allowed to load local resource


브라우저에서는 사용자 로컬에 접근할 수 없음


  • 이클립스에서 JSP로 개발시 파일경로 설정방법 (http://blog.itthis.me/28) 

<img src="${pageContext.request.contextPath}/resources/img/profile.jpg">

<img src="/resources/img/profile.jpg">

http://localhost:8082/resources/img/profile.jpg

비교 http://localhost:8082/mapstar/insert

  •  resources mapping

  • <Context docBase="d:\test_img" path="/local_img" reloadable="true"></Context>    > 시스템 경로 사용 시 

  • java의 File에서 파일을 읽기/쓰기 할때는 제일 쉬운방법은 절대경로(웹경로 x)를 넣는 것이고,

html등의 웹페이지에서 이미지 출력시에는 context의  상대경로/절대경로를 넣어야 합니다.

그리고, 이미지의 경우, WEB-INF 하위로 두게 되면 일반 웹URL로 인식안되니

ROOT 하위로 빼놓으셔야 합니다  > 프로젝트 안으로 

(https://okky.kr/article/317040?note=1043455)

  • Web Browser를 통해서 이미지를 보고자 할때에는

Http 서비스가 가능한 위치여야 합니다. 

일반적으로 Http Server에서 이미지파일이나 스크립트, 또는 css 등의 파일에 접근하기 위해서는

해당 경로가 Http URL로 지정이 되어 있어야 하는거죠...

예로 Tomcat에서 개발을 한경우 Context 로 지정된 폴더및 그 하위의 모든 폴더 들은

Http URL로 접근이 가능하게 됩니다. 

님처럼 특정 폴더에 파일을 올리시고 그것을 Web을 통해서 서비스를 하시려면..

Servlet 단에서 response 로 파일을 보내시면 됩니다. > 아예 파일로 response

해당 예제들은 구글에서 검색하시면 쉽게 찾을수 있을 거예요..

(https://okky.kr/article/417238)

  • 접금권한 자체가 웹 루트부터 시작 하니까요.
    위 글처럼 하시려면직접 파일을 읽어서 스트림으로 내려주면 됩니다.

  (https://okky.kr/article/237812)

MultipartHttpServletRequest 에러



[문제]


파일 업로드시 다음과 같은 에러 


nested exception is java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]



[해결책]

MULTIPART RESOLVERS 를 빼먹었다. 넣어주자 


<!-- MULTIPART RESOLVERS -->  

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="1000000000" />

</bean>




RedirectAttrebutes 에러



[문제]


Spring redirect POST parameter 를 사용중에 다음과 같은 에러가 발생하였다.


nested exception is java.lang.IllegalStateException: 

Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model.

You may need to switch newer MVC infrastructure classes to use this argument.] with root cause



[해결책]

xml 파일에 다음 코드를 추가한다.

해당 코드로 인해 스프링 3.1부터는 Spring Web MVC 를 위해 설정해야 하는 값들이 자동으로 추가 된다.


<mvc:annotation-driven />






JSP 페이지 이동 url 경로 


( url-pattern .do )

 WEB.INF

    - VIEW

       - index.jsp

 - notice

 - noticeList.jsp 

 - noticeInsert.jsp


- index.jsp 에서 


<a href="notice/noticeList.do">NOTICE</a>  http://localhost:8082/mogaco/notice/noticeList.do

<a href="/notice/noticeList.do">NOTICE</a>   http://localhost:8082/notice/noticeList.do

<a href="../notice/noticeList.do">NOTICE</a>  http://localhost:8082/notice/noticeList.do


- noticeList.jsp 에서 


<a href="notice/noticeList.do">NOTICE1</a> http://localhost:8082/mogaco/info/notice/notice.do

<a href="/notice/noticeList.do">NOTICE2</a>  http://localhost:8082/notice/noticeList.do

<a href="../notice/noticeList.do">NOTICE3</a> http://localhost:8082/mogaco/notice/noticeList.do 


- noticeInsert.jsp


<a href="../notice/noticeInsert.do">공지사항 인서트1</a>

<a href="noticeInsert.do">공지사항 인서트2 </a>

<a href="/notice/noticeInsert.do">공지사항 인서트3</a> http://localhost:8082/notice/noticeInsert.do  

<a href="notice/noticeInsert.do">공지사항 인서트4 </a> http://localhost:8082/mogaco/notice/notice/noticeInsert.do


※  아무것도 없으면 프로젝트부터 현재까지 내려옴 

※   / : 현재위치 부터,    ../ : 현재한칸위로 


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

${pageContext.request.contextPath}

절대경로 활용 

+ Recent posts