0
我有一個liferay portlet並使用Spring MVC。我想使用Ajax將一個基本對象發送到控制器。問題是控制器只接收空對象。Ajax向控制器發送空數據
的index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="graphui.GraphUIConstants" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<portlet:resourceURL var="sendPersonURL" id="<%= GraphUIConstants.SEND_PERSON %>" />
<script>
function URL_SEND_PERSON_KEY() { return '${sendPersonURL}'; }
function sendPerson(){
var company = new Object();
company.id = 1;
company.name = "coName";
var person = new Object();
person.name = "James";
person.age = 40;
person.email = "[email protected]";
person.company = company;
var personInJSON = JSON.stringify(person);
AUI().use('aui-io-request', function(A){
A.io.request(URL_SEND_PERSON_KEY(), {
method: 'post',
data: personInJSON,
on: {
success: function() {
alert(this.get('responseData'));
}
}
});
});
}
</script>
<div>
<ul>
<li>${name};</li>
<li>${age};</li>
<li>${email};</li>
</ul>
<button class="btn btn-primary" onclick="sendPerson()"> Send </button>
</div>
Person.java
public class Person {
private String name;
private int age;
private String email;
public Person() {
}
public Person(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Company.java
public class Company {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
GraphUIConstants.java
public class GraphUIConstants {
public static final String SEND_PERSON = "sendPerson";
}
GraphUI-portlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="graphui" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- Spring MVC Message Source -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage" value="true"/>
<property name="basenames">
<list>
<value>content.graph</value>
</list>
</property>
</bean>
</beans>
的liferay-portlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>GraphUI</portlet-name>
<instanceable>true</instanceable>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<ajaxable>true</ajaxable>
<header-portlet-javascript>/scripts/jquery-1.9.1.js</header-portlet-javascript>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
GraphViewController.java
package graphui;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
/**
*
* Controller for VIEW mode of portlet.
*/
@Controller("graphViewController")
@RequestMapping(value = "VIEW")
public class GraphViewController{
private static final String MODEL_KEY_NAME = "name";
private static final String MODEL_KEY_AGE = "age";
private static final String MODEL_KEY_EMAIL = "email";
private Person repository = new Person("pete", 33, "[email protected]");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final Logger logger = Logger.getLogger(GraphViewController.class);
@ActionMapping(GraphUIConstants.SUBMIT_FORM)
public void handleActionRequest(ActionRequest request, ActionResponse response)throws Exception {
logger.info("action phase executed");
if (request.getParameter("name") != null)
repository.setName(request.getParameter("name"));
if (request.getParameter("age") != null)
repository.setAge(Integer.parseInt(request.getParameter("age")));
if (request.getParameter("email") != null)
repository.setEmail(request.getParameter("email"));
}
@RenderMapping
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response, ModelMap model) {
logger.info("render phase executed");
model.addAttribute(MODEL_KEY_NAME, gson.toJson(repository.getName()));
model.addAttribute(MODEL_KEY_AGE, gson.toJson(repository.getAge()));
model.addAttribute(MODEL_KEY_EMAIL, gson.toJson(repository.getEmail()));
return new ModelAndView("index", model);
}
@ResourceMapping(GraphUIConstants.SEND_PERSON)
public void savePerson(@ModelAttribute("person") Person jsonString, ResourceRequest request, ResourceResponse response) {
logger.info("savePerson method was executed");
String name = jsonString.getName();
String age = Integer.toString(jsonString.getAge());
String email = jsonString.getEmail();
String name2 = request.getParameter(MODEL_KEY_NAME);
String age2 = request.getParameter(MODEL_KEY_AGE);
String email2 = request.getParameter(MODEL_KEY_EMAIL);
String name3 = ParamUtil.getString(request, MODEL_KEY_NAME);
String age3 = ParamUtil.getString(request, MODEL_KEY_AGE);
String email3 = ParamUtil.getString(request, MODEL_KEY_EMAIL);
logger.info("Person name is " + name);
logger.info("Person age is " + age);
logger.info("Person email is " + email);
logger.info("Person name is " + name2);
logger.info("Person age is " + age2);
logger.info("Person email is " + email2);
logger.info("Person name is " + name3);
logger.info("Person age is " + age3);
logger.info("Person email is " + email3);
}
}
當我阿賈克斯送Person對象,我在日誌中得到這個消息:
12:52:18,726 INFO [GraphViewController:65] savePerson method was executed
12:52:18,728 INFO [GraphViewController:79] Person name is null
12:52:18,728 INFO [GraphViewController:80] Person age is 0
12:52:18,729 INFO [GraphViewController:81] Person email is null
12:52:18,729 INFO [GraphViewController:83] Person name is null
12:52:18,729 INFO [GraphViewController:84] Person age is null
12:52:18,729 INFO [GraphViewController:85] Person email is null
12:52:18,730 INFO [GraphViewController:87] Person name is
12:52:18,731 INFO [GraphViewController:88] Person age is
12:52:18,731 INFO [GraphViewController:89] Person email is