Spring MVC(十)使用Ajax

来源:互联网 发布:c语言main是什么意思 编辑:程序博客网 时间:2024/04/28 02:38

1.AjaxController.java

package com.yw.controller;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;//Ajax调用springmvc的方法://直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面//url:http://127.0.0.1:8080/testAjax/test/toAjax.do  @Controller@RequestMapping("/test")public class AjaxController {/**      *       * ajax的请求返回值类型是void,参数列表里直接定义HttpServletResponse,      * 获得PrintWriter的类,最后可把结果写到页面 ,可以方便设置编码     */      @RequestMapping("/ajax1.do")      public void ajax1(String name, HttpServletResponse response) {      response.setCharacterEncoding("utf-8");        String result = "hello " + name;          try {              response.getWriter().write(result);          } catch (IOException e) {              e.printStackTrace();          }      }        /**      *       * 直接在参数的列表上定义PrintWriter,out.write(result);      * 把结果写到页面     * @throws UnsupportedEncodingException      *       */      @RequestMapping("/ajax2.do")      public void ajax2(String name, PrintWriter out) throws UnsupportedEncodingException {          System.out.println("name="+name);//    String result = "===hello== " + new String(name.getBytes("ISO-8859-1"),"utf-8");  //        System.out.println("result="+result);//        out.write(result);          out.write(name);      }      /**      * 转向ajax.jsp页面      */        @RequestMapping("/toAjax.do")        public String toAjax() {          return "ajax";     }  }

2.ajax.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'ajax.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript" src="js/jquery-1.9.0.js"></script><script type="text/javascript">$(function() {$("#mybutton").click(function() {$.ajax({url : "test/ajax1.do",type : "post",dataType : "text",data : {name : $("#name").val()},success : function(responseText) {alert(responseText);},error : function() {alert("system error");}});});$("#mybutton2").click(function() {$.ajax({url : "test/ajax2.do",type : "post",dataType : "text",data : {name : $("#name").val()},success : function(responseText) {alert(responseText);},error : function() {alert("system error");}});});});</script></head><body>name:<input type="text" name="username" id="name"><input id="mybutton" type="button" value="click1"><input id="mybutton2" type="button" value="click2"></body></html>

3.firest-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: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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.yw"></context:component-scan><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/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>

4.web.xml和application.xml和前一篇文章例子完全一样

5.运行



输入name点击click1按钮

点击click2按钮


0 0
原创粉丝点击