Struts - ajax/json

2015. 5. 20. 14:48plming/Java - Struts

Struts.xml에

json으로 응답받을 Action.method에 result값으로 json을 처리할 type을 기술하고

화면에서 받을 이름을 기술했는데도 정상적으로 값이 읽혀지지 않을 때는

방황하지 말고, 당황하지 말고, 삽질하지 말고

해당 Action에 get/set method를 생성해주어야

화면에서 정상적으로 값을 읽을 수 있다.

 

화면에서 읽을 수 있도록

HttpServletRequest request =  ServletActionContext.getRequest();
request.setAttribute("json값", json값);

요롷게 해줘도 읽혀지지 않는다.

${json값.Key1} 요롷게는 읽혀져도...

 

- Struts.xml

<result-type name="json처리" class="json처리Class">
     <param name="encoding">UTF-8</param>
     <param name="contentType">text/html</param>
     <param name="contentType">application/json</param>
</result-type>

 

<action name="getJsonValue" class="Action" method="jsonMethod">
     <result name="success" type="json처리"><param name="value">json값</param></result>
</action>

 

- Action.java

private Map<String, Object> json값;

 

public String jsonMethod() {

    json값 = 블라블라;

    return SUCCESS;

}

 

public Map<String, Object> getJson값() {
     return json값;
}
public void setJson값(Map<String, Object> json값) {
     this.json값 = json값;
}

 

 

- 화면.jsp

$.ajax({
    type:'POST',
    url : "/getJsonValue.do",
    data :  { "param1" : param1 },
    success : function( json ) {
        var json값 = eval( json );
        alert( json값.Key1 );
    }
});

 

'plming > Java - Struts' 카테고리의 다른 글

404 페이지 대신 Struts Problem Report 나올 때  (0) 2015.07.27
Struts2 - Login 처리  (0) 2015.05.20