零基础学习java------39---------

一. json格式交互(知道)

1 . 回顾ajax基本语法

$.ajax({
    url:"",  // 请求的后台路径
    data:{"":"","":""} // 请求携带的数据
    type:"", // 请求方式
    dataType:"json" //接收后台返回数据的解析方式
    success:function(data){}//请求成功时的回调函数        
})

2. 代码

2.1 测试部分代码

前提:环境搭建

创建web项目--->导入需要的jar包---->配置web.xml---->配置用于获取springmvc容器的配置文件(在web.xml中的前端控制器中加载,并放在conf文件夹里)

补充知识:web-inf下的静态文件的访问

首先,对于外部访问来说,web-inf下的文件都是不可见的(即不能通过url获得web-info下的任何文件),所以,直接访问jsp是不可能的。这要从web-info文件夹的作用说起:
WEB-INF的存在以及其下的lib和classes目录的作用都是jsp规定的,主要是系统运行的配置信息和环境,用来存储服务端配置文件信息和在服务端运行的类文件,它下面的东西不允许客户端直接访问的,这是jsp环境的规定。
而我们通常是使用view层框架(如struts)来提供jsp服务,此时,我们可以将jsp文件放到web-info下避免客户直接访问到页面,同时使用struts来进行jsp文件提取,并将编译好的结果发送到客户端。

所以,此处的js文件不能放进web-inf中,若想放进去需要进行静态资源的配置

项目结构图:

 前端代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function(){
    alert("hello")
})
</script>
<body>
</body>
</html>
View Code

web.xml文件

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>day18</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置前端控制器 -->  
  <servlet> 
    <servlet-name>springmvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <!-- 在创建servlet对象时,初始化其值 -->  
    <init-param> 
      <!-- 指定springmvc配置文件的路径,如果不指定,则读取web-inf下    servlet的名称-servlet.xml   -->
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:springmvc.xml</param-value> 
    </init-param>   
    <!-- 值大于0,就自动创建servlet实例,数字越小越先加载 -->  
    <load-on-startup>2</load-on-startup> 
  </servlet>  
  <servlet-mapping> 
    <servlet-name>springmvc</servlet-name>  
    <url-pattern>*.do</url-pattern> 
  </servlet-mapping> 
  
  <!-- 配置post方式的字符过滤器解决中文乱码问题 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
View Code

springmvc.xml

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!--处理器适配器和处理器映射器 json 数据解析组件 -->
    <mvc:annotation-driven />
    <!-- spring的IOC的扫码组件 -->
    <context:component-scan base-package="com._51doit.controller"/>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/html/" />
        <property name="suffix" value=".html" />
    </bean>
</beans>
View Code

PageController

@Controller
public class PageController {
    @RequestMapping("ajax")
    public String  showAjax() {
        return "ajax_demo";
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/jj1106/p/11707091.html