【发布时间】:2015-08-13 09:17:02
【问题描述】:
我正在使用 liferay 6.2 CE ga4,并使用 Maven (MVC)。
这是我的代码:
国家:
<select id="country" name="country">
<option value="select">Select Country</option>
<option value="india">India</option>
<option value="usa">USA</option>
</select>
State:
<select id="state" name="state">
</select>
<script type="text/javascript">
$(document).ready(function(){
$( "#country" ).change(function() {
var country = $(this).val();
$.ajax({
url: "${findState}" ,
type: 'POST',
datatype:'json',
data: "countryName="+country ,
success: function(data){
$('#state').html('');
$.each(content, function(i, state) {
$('#state').append($('<option>').text(state.name).attr('value', state.stateId));
});
}
});
});
});
</script>
这是我的 Java:
@ResourceMapping(value="findState")
public void findStateForCountry(ResourceRequest request, ResourceResponse response) throws IOException {
String countryName = ParamUtil.getString(request, "countryName");
System.out.println("name="+countryName);
String name = request.getParameter("countryName");
System.out.println("name2="+name);
//var countryName and var name allways NULL
JSONArray stateArray = JSONFactoryUtil.createJSONArray();
JSONObject stateObject,stateObject2;
if(countryName.equalsIgnoreCase("india"))
{
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "1");
stateObject.put("name", "Delhi");
stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "2");
stateObject2.put("name", "Gujrat");
}
else{
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "21");
stateObject.put("name", "LA");
stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "22");
stateObject2.put("name", "California");
}
stateArray.put(stateObject);
stateArray.put(stateObject2);
response.getWriter().println(stateArray);
}
我想使用 pass 参数,但它不起作用。我用这个:
String countryName = ParamUtil.getString(request, "countryName");
还有这个:
String name = request.getParameter("countryName");
要获取参数但他们总是得到NULL。
我的代码有问题吗?
答案是 :
在你的 liferay-portlet.xml 中使用它
<portlet> <portlet-name>welcome</portlet-name> <requires-namespaced-parameters>false</requires-namespaced-parameters> </portlet>
【问题讨论】:
标签: java ajax spring maven spring-portlet-mvc