我在jsp页面设置receiverid。我正在使用ajax发送textarea值
并在相同的方法我得到正常工作的客户端IP。
我也想获得在jsp页面中设置的receiverid。
但这显示NullPointerException
。
在jsp中
<s:set var="receiverid" value="1" scope="request"/>
<form action="" id="post_message_form">
<s:textarea name="message"></s:textarea>
<button type="submit" id="submit_status_button">Submit</button>
</form>
$(document).on('submit', '#post_message_form', function(f) {
f.preventDefault();
var formData = $("#post_message_form").serialize();
console.log(formData);
$.ajax({
type: 'POST',
url: 'PostIt',
data: formData,
dataType: "text html",
success: function(data) {
}
})
});
行动中
public class UserMessageInsert extends ActionSupport implements ModelDriven<PostMessages> {
PostMessages pm = new PostMessages();
private long aid;
public String insert() {
HttpServletRequest request = ServletActionContext.getRequest();
String ipAdd = request.getHeader("X-FORWARDED-FOR");
if (ipAdd == null) {
ipAdd = request.getRemoteAddr();
long l=(Long) request.getAttribute("receiverid");//NulllPointer Exception
System.out.println("Ip is "+ipAdd+" l is "+getAid());
}
}
答案 0 :(得分:0)
我想问题是AJAX调用忽略了你的请求范围属性,因为它创建了一个全新的XMLHttpRequest而不是经典的HttpRequest。
只需将您的属性添加为表单中的隐藏参数,除非您有理由不这样做:
<s:set var="receiverid" value="1" scope="request"/>
<form action="" id="post_message_form">
<s:hidden value="%{#request.receiverid}" name="receiverid" />
<s:textarea name="message"></s:textarea>
<button type="submit" id="submit_status_button">Submit</button>
</form>
然后它会被序列化并提交,您将使用setter或getParameter()
而不是getAttribute()
来访问它。