1. 新建web project
2. 增加jar
3. 改写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4. 新建config包下的spring-servlet.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"> <!-- 注解扫描包 --> <context:component-scan base-package="com.tgb.web.controller" /> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <!-- ViewResolver 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
5. 新建UserController.java, 在com.tgb.web.controller包下
package com.tgb.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.tgb.web.controller.entity.User; @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/addUser") public String addUser(User user,HttpServletRequest request){ String result = "this is addUser---------优化版"; request.setAttribute("userName", user.getUserName()); request.setAttribute("age", user.getAge()); return "/userManager"; } @RequestMapping("/addUserJson") public void addUserJson(User user,HttpServletRequest request, HttpServletResponse response){ String result = "{"userName":" "+ user.getUserName()+" ", "age":" "+ user.getAge()+" "}"; PrintWriter out = null; response.setContentType("application/json"); try { out=response.getWriter(); out.write(result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping("/toUser") public String toUser(){ return "/json"; } }
6. 创建json.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.2.min.js"></script> <title>My JSP 'addUser.jsp' starting page</title> <script type="text/javascript"> $(document).ready(function(){ $("#add").click(function(){ var userName=$("#userName").attr("value"); var age=$("#age").attr("value"); var user={userName:userName,age:age}; $.ajax({ url:"/springMVC5/user/addUserJson", type:"POST", data: user, success:function(data){ <%--alert("userName--->"+data.userName +"age--->"+data.age);--%> document.getElementById("div").innerHTML="<font color='red'>"+data.userName+data.age+"</font>"+"添加成功"; } }); }); }); </script> </head> <body> <h> json添加用户 </h> <br> 姓名: <input type="text" id="userName" name="userName"> 年龄: <input type="text" id="age" name="age"> <br/> <input type="button" id="add" value="添加"> <div id="div"> </div> </body> </html>
7.IE端验证
http://localhost:8080/springMVC5/user/toUser