我已经完成了与此主题相关的绝对每一篇文章,尝试了所有建议的选项......几天之后,它仍然不适合我。
简而言之,我想做的是:
第1点正常工作,我设法发送POST请求并在控制器端正确接收。什么不起作用是从控制器回到jsp和为$ .post()定义的函数的答案。我在Web浏览器端调试它,我可以看到响应的Content-Type不是" application / json" (因此错误406,我相信)。
以下是我所拥有的:
JSP:
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="springForm"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"/>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
</head>
<body>
<springForm:form method="POST" commandName="signInDto"
action="/EquipmentManager/signIn.do" id="add-user-form">
<p>
Login:
<springForm:input path="login" />
<springForm:errors path="login" cssClass="error" />
</p>
<p>
Password:
<springForm:password path="password" />
<springForm:errors path="password" cssClass="error" />
</p>
<p>${errorMessage}
<p />
<input type="submit" value="Sign In" />
</springForm:form>
<script type="text/javascript">
function collectFormData(fields) {
var data = {};
for (var i = 0; i < fields.length; i++) {
var $item = $(fields[i]);
data[$item.attr('name')] = $item.val();
}
return data;
}
$(document).ready(
function() {
var $form = $('#add-user-form');
$form.bind('submit', function(e) {
// Ajax validation
var $inputs = $form.find('input');
var data = collectFormData($inputs);
$.post('/EquipmentManager/signInValidate', data,
function(response) {
$form.find('.control-group').removeClass(
'error');
$form.find('.help-inline').empty();
$form.find('.alert').remove();
if (response.value == 'FAIL') {
alarm(response.value);
} else {
$form.unbind('submit');
$form.submit();
}
}, 'json');
e.preventDefault();
return false;
});
});
</script>
</body>
</html>
控制器:
@RequestMapping(value =&#34; / signInValidate&#34;,method = RequestMethod.POST) public @ResponseBody TestObject signInValidate(@Valid SignInDto signInDto, BindingResult bindingResult){ TestObject testObject = new TestObject();
if (bindingResult.hasErrors()) {
testObject.setValue("FAIL");
logger.info("FAIL");
} else {
testObject.setValue("SUCCESS");
logger.info("SUCCESS");
}
return testObject;
}
豆:
public class TestObject {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Dispatcher Servlet:
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.equipment.controller" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.esocphotoclub</groupId>
<artifactId>EquipmentManager</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>EquipmentManager Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>EquipmentManager</finalName>
</build>
</project>
我已经尝试过不同版本的jackson- *,明确定义ContentNegotiatingViewResolver,显式规范产生=&#34; application / json&#34;在@RequestMapping,TestObject序列化,以及我在这里找到的其他一些东西。它们都不适合我。
为了尽可能多地提供您的信息,如果我没有请求&#39; json&#39;键入$ .post并在控制器中返回一个简单的字符串。但这不是我想要做的。我想要的是提交一个验证目的请求并接收一个对象,而不是一个简单的字符串。
任何暗示我都非常感激,也许还有我还没有尝试过的东西。
非常感谢提前!
答案 0 :(得分:0)
我希望你有所有杰克逊映射,如果是这样你就错过了jsonview。你能试试下面的吗?我们所做的只是设置多个视图解析器。
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="jsonViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
在WEB-INF文件夹中创建一个名为views.xml的文件,并添加以下条目。
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
答案 1 :(得分:0)
我花了几天时间,但我终于通过反复试验方法解决了这个问题。
我所做的只是在POM文件中添加另一个jackson依赖项: jackson-databind ,现在它可以正常工作。