jsp页面如下:
<form id="myForm" method="post" action="/mvc/student/add">
<table>
<tr>
<td>姓名</td>
<td><input id="name" name="name" value="${name}" /></td>
</tr>
<tr>
<td>性別</td>
<td>男<input type="radio" name="sex" value="1" />女<input
type="radio" name="sex" value="0" />
</td>
</tr>
<tr>
<td>祖籍</td>
<td><select name="home">
<option value="0">请选择</option>
<option value="云南">云南</option>
<option value="湖南">湖南</option>
<option value="广东">广东</option>
</select></td>
</tr>
<tr>
<td><input type="button" value="保存" οnclick="validate()" /></td>
</tr>
</table>
</form>
jsp中的js方法如下:
<script type="text/javascript">
function validate(){
var name = $("#name");
if(null == name.val() || "" == name.val()){
alert("请输入姓名!");
name.focus();
return;
}
var sex = $('input[name="sex"]:checked');
if(sex.length == 0){
alert("请选择性别!");
$('input[name="sex"]')[0].focus();
return;
}
var home = $('select[name="home"]').find('option:selected');
if(home.val() == 0){
alert("请选择祖籍!");
$('select[name="home"]').focus();
return;
}
$.ajax(
{
url: "/mvc/student/add",
type:"POST",
//data:"name="+name.val()+"&sex="+sex.val()+"&home="+home.val(),
data:{
name:name.val(),
sex:sex.val(),
home:home.val()
},
dataType:'text',
success: function(result){
alert(result);
}
}
);
}
</script>
以上两种格式的data经过测试都可以请求。
Controller代码如下:
@Controller
@RequestMapping("student")
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping(value="add",method={RequestMethod.GET})
public String add() {
return "addStudent";
}
@RequestMapping(value="add",method={RequestMethod.POST})
@ResponseBody
public Student add(Student student) {
return studentService.add(student);
}
}
最重要的一个地方,就是springmvc配置文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <description>springmvc配置</description> <!-- 自动扫描且只扫描@Controller,service必须在application中加载好并且加上事务,否则此时关联到的service没有事务。--> <context:component-scan base-package="com.*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
以上配置文件几乎没任何地方提到对于json的处理。网上查了很多例子,都提到需要在Adapter中配置Converter。但我这里的配置真没有像那样处理,我猜想跟版本有关系,得空再调试看看spring内部是如何自处理的。或许那样的配置有什么特别的之处,我现在还没接触到。唯一不同地方是引入的jar包。
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.0.0</version> </dependency>