步骤一:在pom.xml中添加如下依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
步骤二:在我们自定义的spring.xml配置文件中添加mvc:annotation-driven/注解(具体见代码中的第4步):
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--1.连接数据库-->
<bean id="db" class="com.alibaba.druid.pool.DruidDataSource">
<property name="password" value="root"/>
<property name="username" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/day06"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--2.扫描注解包-->
<context:component-scan base-package="com"></context:component-scan>
<!--3.创建视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--4.加载注解驱动-->
<mvc:annotation-driven/><!--本案例重点-->
<!--5.创建sqlsession工厂-->
<bean id="fac" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="db"></property>
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
</bean>
<!--6.扫描dao层接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.dao"></property>
<property name="sqlSessionFactoryBeanName" value="fac"></property>
</bean>
<!--7.事务-->
<bean id="mytx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="db"></property>
</bean>
<tx:annotation-driven transaction-manager="mytx"></tx:annotation-driven>
<!--8.静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--使用注解授权的第一个步骤:在子容器中加载该注解授权,不能再父容器中加载,因为子容器可以访问父容器,父容器不能访问子容器。因此该案例中开启aop对类代理、开启shiro注解支持这两步一定不能放在spring-shiro.xml文件中,因为他是父容器。 DispatcherServlet加载的文件属于子容器。ContextLoaderListener加载的文件属于父容器,具体见web.xml文件-->
<!-- 开启aop对类代理。记得添加上面的aop命名空间 -->
<aop:config proxy-target-class="true"></aop:config>
<!-- 开启shiro注解支持 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="manager"></property><!--除了ref属性值manager是上面SecurityManager对象的id值外其他都是固定搭配-->
</bean>
</beans>
步骤三:在相应的请求地址处理方法上加@ResponseBody即可。记得json数据要用对象传给前台,也就是说要为json数据创建一个相应的实体类,该实体类的属性就是json数据的key,json数据的value用实体类的set方法存给对象,用return 实体类将json数据传给前台。
如封装json数据的实体类,起名为:Message
public class Message {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
controller层处理前台请求的方法,起名为:PhoneController
@Controller
public class PhoneController{
@RequestMapping("/getphonecity")
@ResponseBody //@ResponseBody 注解
public Message getcity(String phone){//前台Ajax传过来的数据,前后台数据名必须一样才能接收到。该案例使用根据号码查询归属地。
Message message=new Message();
message.setStr("天津");
return message;//返回实体类,前台Ajax的data接受的就是message对象,直接用data.getStr()即可获取存储的数据。
}
}
特别说明:前台如何使用ajax