Ajax练习一(配置Java后台)

(一)配置Java后台


  这里为了简单,我直接用SpringMvc配置后台(这个配置网上一搜一大把)。

1.导包
  1. 下载Spring相关Jar包和安装SpringIDE的参考资料(讲的很详细)
  2. 查找依赖或下载相应的Jar(很全)
  3. 创建Javaweb项目的教程(创建Maven项目的就不贴出来了,意义不大)

  接下来向lib中,导入如下图所示的jar。这里有几个Jar除了commons-logging我没标记。剩下三个都是用来返回Json格式的数据。

2.配置web.xml配置文件

  web.xml文件路径:/WebContent/WEB-INF/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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MineTest</display-name>
	<!-- 配置SpringMvc前端控制器 -->
     <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_mvc.xml</param-value>
		</init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>springMvc</servlet-name>
      <url-pattern>*.do</url-pattern>
   </servlet-mapping>
  <!-- 设置欢迎页面 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
3.创建并配置spring_mvc.xml配置文件

  在/src目录下创建spring_mvc.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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 设置要扫描的包 -->
	<context:component-scan base-package="com.lyan.web.controller"/>
	<!-- 这里用于与JackJson返回Json数据 -->
	<mvc:annotation-driven/>
</beans>
4.创建Controller用于接收请求

  创建Controller要将他放在指定的扫描包中(对应着spring_mvc.xml中的【context:component-scan base-package=“com.lyan.web.controller”】)。

@Controller
public class TestController {

	@ResponseBody
	@RequestMapping(value = "/first" , method = RequestMethod.GET)
	public Person firstRequest(){
		System.out.println(TimeUtils.getYearDate(System.currentTimeMillis()));
		Person person = new Person();
		person.setName("美女");
		person.setAge(20);
		person.setSex('女');
		return person;
	}
}
5.运行项目查看结果

  运行项目,在浏览器中的请求地址后加上“first.do”,然后回车。测试一下get请求的结果是否显示到界面中。请求成功的结果如下图所示:
结果

  到这里后台的准备工作基本完成!接下来将使用。原生JS实现Ajax异步请求

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值