SringMVC Ajax

1.项目结构

2.user_info.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jQuery1.9.1.js"></script>
<script type="text/javascript">
    $().ready(function() {
        
        $("#but01").click(function() {
            var userNumber=$("#userNumber").val();
            if(userNumber.length==0){
                alert('请输入账号!');
            }else {
                $.post("ajaxUser.do",{userNumber:userNumber},function(data){
                    alert(data);
                })
            }
        });
    });
    
    $().ready(function() {
        
        $("#but02").click(function() {
            var userId=$("#userId").val();
            if(userId.length==0){
                alert('请输入账号!');
            }else {
                $.post("ajaxUser2.do",{userId:userId},function(data){
                    alert(data.userId+"---"+data.userName+"---"+data.userAge);
                },"json")
            }
        });
    });
    
</script>
</head>
<body>

<hr>
账号:<input type="text" id="userNumber" name="userNumber"/>&nbsp;
    <input type="button" id="but01" value="验证">

<hr/>
账号:<input type="text" id="userId" name="userId"/>&nbsp;
    <input type="button" id="but02" value="登录">

<hr/>
</body>
</html>

3.UserController.java代码

package com.spring;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.po.UserInfor;

@Controller
public class UserController {
    @RequestMapping("/ajaxUser.do")
    public void userNumber(String userNumber,HttpServletResponse response){ 
        
        try {
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            PrintWriter out=response.getWriter();
            if ("dong".equals(userNumber)) {
                out.println("对不起,请重新输入账号!");
            }else {
                out.println("恭喜您,账号可以使用!");
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println(userNumber);
    }
    
    
    @RequestMapping("/ajaxUser2.do")
    public @ResponseBody UserInfor ajaxUser(Integer userId){
        
        System.out.println("编号:"+userId);
        UserInfor user=new UserInfor();
        user.setUserId(userId);
        user.setUserName("董诗原");
        user.setUserAge(18);
        return user;
    }

}

@ResponseBody用来返回对象

3.springmvc-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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 ">
    <!-- Springmvc注解驱动 -->    
    <mvc:annotation-driven />    
    <!-- 扫描器 -->
    <context:component-scan base-package="com.spring" />
    
    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    
    
    <!-- 从请求响应读取/编写字符串 -->
    <bean id="stringHttpMessage" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    
    <!-- 用于对象转化成json -->
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringHttpMessage"/>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>
    
</beans>

  增加请求响应读取、字符集、对象转换

4.视图

 

 

posted @ 2018-01-05 18:47  2016024291-董诗原  阅读(289)  评论(0编辑  收藏  举报