最近学习了一下spring mvc,其简单的配置和对ajax的支持让笔者真是眼前一亮,
在这里记录下来方便以后查询
首先定义UserController类,相当于struts里德action类
package com.alfa.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alfa.model.Mail;
import com.alfa.model.User;
import com.alfa.service.FrontDesk;
@Controller
public class UserController {
@Autowired
private FrontDesk frontDesk;
@RequestMapping("/user/addTest")
public @ResponseBody User addTest(){
System.out.println("xxx");
User user = new User();
user.setId(1);
user.setUsername("xxx");
return user;
}
@RequestMapping("/user/addTest1")
@ResponseBody
public String addTest1(){
System.out.println("xxx");
User user = new User();
user.setId(1);
user.setUsername("xxx");
return "yyy";
}
@RequestMapping("/user/addTest2")
@ResponseBody
public List<User> addTest2(){
System.out.println("xxx2");
List<User> users = new ArrayList<>();
User user = null;
for (int i = 0; i < 5; i++) {
user = new User();
user.setId(i);
user.setUsername("xxx"+i);
users.add(user);
}
return users;
}
@RequestMapping("/user/addTest3")
@ResponseBody
public Map<String, Object> addTest3(){
System.out.println("xxx");
Map<String, Object> map = new HashMap<>();
map.put("str", "xxx");
map.put("csg", "xxx");
return map;
}
@RequestMapping("/user/addTest4")
@ResponseBody
public boolean addTest4(User user){
System.out.println(user.getId());
System.out.println(user.getUsername());
return true;
}
}
在src目录下创建一个spring-servlet.xml文件,也可以从下载的spring文档里copy,类似于struts.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<!-- 自动扫描action包里的带有@Contaoller注解的类-->
<context:component-scan base-package="com.alfa.action"></context:component-scan>
<mvc:annotation-driven/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射请求映射-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
接下来就是web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
核心的就是这些配置,页面上的触发
javascript 代码
$.ajax({
type:'POST',
url: '${ctx}/user/addTest.html',
dataType : 'json',
success: function(data){
alert("Data Saved: "+data.username);
},
error:function(data){
alert("error1");
}
});
本文介绍如何使用Spring MVC进行基本配置,并展示如何利用其对AJAX的支持实现前端与后端交互。通过定义Controller类、创建配置文件、设置web.xml,实现了简单请求处理与响应。此外,演示了如何通过AJAX发送POST请求到Spring MVC服务器,成功保存数据并显示返回值。
3532

被折叠的 条评论
为什么被折叠?



