springmvc+ajax——第一讲(搭建)

下面是整个整合测试的代码:

ajax01.html

TestController

web.xml

springmvc.xml

applicationContext.xml

<!DOCTYPE html>
<html>
  <head>
    <title>index.html</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=gb2312">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

<script type="text/javascript" charset="UTF-8">

var param = "ajaxget";
var xmlHttpRequest = null;//作为全局变量,否则在回调函数中报notdefined
function ajaxSubmit(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
}

if(xmlHttpRequest != null){
//document.write("xxx");
xmlHttpRequest.open("GET", "./testAjax?param="+param, true);
xmlHttpRequest.onreadystatechange = ajaxCallBack;//没有括号
xmlHttpRequest.send(null);
}
}

function ajaxCallBack(){
if(xmlHttpRequest.readyState == 4){
if(xmlHttpRequest.status == 200){
var responseText = xmlHttpRequest.responseText;//没有括号
document.getElementById("div1").innerHTML = responseText;//innerHTML没有括号
}
}
}

</script>
  </head>
  
  <body>
 
  <input type="button" value="check" id="check" onclick="ajaxSubmit();">
<br>
<div id="div1"></div>

  </body>
</html>
package priv.lirenhe.js.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 org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
public TestController() {
System.out.println("------TestController------");
}
@RequestMapping(value="/testAjax",method=RequestMethod.GET)
public void testAjax(HttpServletRequest req,HttpServletResponse resp){
System.out.println("testAjax");
String param = null;
param = req.getParameter("param");
System.out.println(param);
PrintWriter pw = null;
try {
pw = resp.getWriter();
pw.print(param);
} catch (IOException e) {
e.printStackTrace();
}finally{
pw.flush();
pw.close();
System.out.println("finally");
}
}
}

web.xml配置:注意不要使用<web:xxx>的标签

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>js_001</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  <filter>
    <filter-name>encoding</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>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

springmvc.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 必须有带mvc的 -->
<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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="priv.lirenhe.js.*"></context:component-scan>
<mvc:annotation-driven />
<mvc:resources location="/" mapping="/**"/>
<bean id="modelAndView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<constructor-arg name="prefix" value="/"/>
<constructor-arg name="suffix" value=".html"/>
</bean>
</beans>
<!-- 
包扫描器
注解驱动
静态资源
前缀后缀
 -->

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

</beans>

 

springmvc+ajax——第一讲(搭建)

如果不能加载的话就是web.xml配置的问题。


上一篇:Lua获取网络时间


下一篇:一些常用css技巧的为什么(一)我所理解的margin