我做了一个这样的框架:
spring+hibernate+spring mvc
想让spring mvc返回json数据,使用了json-lib-ext-spring1.0.2。
刚弄完时是http://localhost/listPerson.do可以在页面上显示出json数据的,但是后来改了一些东西。然后再调用http://localhost/listPerson.do却不显示json数据了,而是提示下载listPerson.do。请教大家是怎么回事啊?
代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring Annotation MVC Sample</display-name>
<!-- Spring 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param><!-- Spring 容器启动监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>json</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>json</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app>
applicationContext.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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> <!-- bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/ --> <!-- <context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、 CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。 使用这个选项必须加入最上面的3、6、7项 --> <context:annotation-config/> <!-- <bean id="boss" class="net.sf.test.Boss"/> <bean id="office" class="net.sf.test.Office"> <property name="no" value="001"/> </bean> <bean id="car" class="net.sf.test.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean> --> <aop:config> <aop:advisor pointcut="execution(* net.sf.service.*.*(..))" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="insert*" /> <tx:method name="update*" /> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /--> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <property name="driver"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="driverUrl"><value>jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))</value></property> <property name="user" value="test"></property> <property name="password" value="test"></property> <property name="alias" value="myblogdb"></property> <!--property name="houseKeepingSleepTime"><value>9000</value></property--> <property name="prototypeCount" value="5"></property> <property name="maximumConnectionCount" value="100"></property> <property name="minimumConnectionCount" value="10"></property> <property name="trace" value="true"></property> <property name="verbose" value="true"></property> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="ORACLE" /> <property name="generateDdl" value="true" /> <property name="showSql" value="true" /> </bean> </property> </bean> <context:component-scan base-package="net.sf"> <!--context:include-filter type="aspectj" expression="edu.jlu.fuliang.util..*"/--> <context:include-filter type="regex" expression="net\.sf\.service\..*"/> <context:include-filter type="regex" expression="net\.sf\.test\..*"/> <context:exclude-filter type="regex" expression="net\.sf\.action\..*"/> </context:component-scan>
</beans>
json-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="net.sf.action"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"/>
</beans>
views.properties
jsonView.(class)=net.sf.json.spring.web.servlet.view.JsonView
jsonView.contentType=application/json;charset=UTF-8
PersonAction.java
package net.sf.action;import net.sf.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class PersonAction {
@Autowired
private PersonService _personService;@RequestMapping("/listPerson.do") // <—— ① public String listPerson(Model m) { //_personService.getList(); m.addAttribute("personList", _personService.getList()); System.out.println("call listAllBoard method.ss"); return "jsonView"; }
}
PersonServiceImpl.java
package net.sf.service.impl;import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;import net.sf.service.PersonService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;@Scope("prototype")
@Service("personService")
public class PersonServiceImpl implements PersonService {
EntityManager em;@PersistenceContext public void setEm(EntityManager em) { this.em = em; } public List getList(){ String sHql = "FROM Person"; Query query = em.createQuery(sHql); List resultList = query.getResultList(); return resultList; } public String test(){ return "hello world, 雪狐!"; }
}