3
아래 코드를 사용하여 국가의 프런트 엔드 onchange에서 ajax를 통한 드롭 다운에서 countryCode를 표시했습니다. 그러나 json 결과를 얻을 수 없습니다. 프런트 엔드에 대한 액션 클래스. 아무도 나를이 문제를 해결하기 위해 도와 드릴까요?struts2 작업 메서드에서 json 결과를 얻는 방법 (jquery 사용)
Struts.xml 파일 :
<result-types>
<result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
</result-types>
<action name="populateCountryCode"
class="com.CustRegnManagerAction "method="populateCountryCode">
<result name="success" type="json">
<param name="root">jsonData</param>
</result>
</action>
Action 클래스 방법 :
public String populateCountryCode() throws Exception
{
jsonData = new HashMap<String, String>();
try{
if(!getCountry().equals("") && getCountry()!=null){
List<Country> countryCodeNew =(List<Country>) custRegnManagerService.getCountryCode(getCountry());
for(Country country: countryCodeNew){
countryCode=country.getCountryCode();
jsonData.put("countryCode", countryCode);
// setJsonData(jsonData);
}
System.out.println(jsonData);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return SUCCESS;
}
JQuery와 :
$(document).ready(function() {
//checks for the button click event
$("#country").change(function(e){
var countryCode=$('#countryCode').val();
var country=$('#country').val();
$.getJSON(
'populateCountryCode.action' ,
{
country: country
},
function(json) {
alert("hi") ****//not displaying
$('#countryCode').html(json.countryCode);
countryCode=json.countryCode;
}
);
return false;
});
});
JSP 코드 :
<s:form action="addRegistFeedback" method="post" id="theform" >
<s:select headerKey="-1" headerValue="--- Select ---"
label="Country" id="country" cssStyle="width:100%" list="countries"
required="true" name="country"> </s:select><br>
<table style="width: 25%">
<tr style="width: 100%">
<td ><s:label theme="simple">Phone :</s:label>
</td>
<td style="width: 70%">
<s:textfield name="customer.countryCode"id="countryCode" cssStyle="width:20%"
theme="simple" required="true" />
<s:textfield name="stateCode" cssStyle="width:20%" id="stateCode"
theme="simple" required="true" />
<s:textfield name="cityCode" cssStyle="width:45%" id="phone"
theme="simple" required="true" />
</td>
</tr>
</table><br>
<s:token name="token"></s:token>
<s:submit value="Save" id="button" theme="simple" cssStyle="width:50px;"></s:submit>
</s:form>
오류 또는 원하지 않는 출력을 게시 할 수 있습니까? 이렇게하면 문제를 더 잘 이해하고 도움을 줄 수 있습니다. –
함수 (function (json) {$ ('countryCode') .html (json.countryCode);})는 아무것도 표시하지 않습니다. 즉, jsp 페이지 안에 아무 값도 반환하지 않습니다 ... – keplar
표시 할 수 있습니다. 콘솔에서 해당 국가에 대한 countryCode ... 어떤 구성 문제 또는 무게인가? ... 내가 jquery 함수 (이전 의견에서 언급 한) 안에 경고 메시지를 표시하려고 bt 표시되지 않습니다 .. 왜? 그 기능 자체가 작동하지 않습니다. – keplar