Leon1509
2009-09-18 23:48
浏览 270
已采纳

请教关于spring mvc中使用json-lib-ext-spring返回json的问题

我做了一个这样的框架:
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>
&lt;!--  Spring 容器启动监听器 --&gt;
&lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener
    &lt;/listener-class&gt;
&lt;/listener&gt;
&lt;servlet&gt;
    &lt;servlet-name&gt;json&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;json&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

</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">
&lt;!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --&gt;
&lt;!--
    bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/
--&gt;
&lt;!--
    &lt;context:annotationconfig/&gt; 将隐式地向 Spring 容器注册
    AutowiredAnnotationBeanPostProcessor、
    CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor
    以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
    使用这个选项必须加入最上面的3、6、7项
--&gt;
&lt;context:annotation-config/&gt;
&lt;!--
    &lt;bean id="boss" class="net.sf.test.Boss"/&gt;
    &lt;bean id="office" class="net.sf.test.Office"&gt; &lt;property name="no" value="001"/&gt; &lt;/bean&gt;
    &lt;bean id="car" class="net.sf.test.Car" scope="singleton"&gt;
        &lt;property name="brand" value=" 红旗 CA72"/&gt;
        &lt;property name="price" value="2000"/&gt;
    &lt;/bean&gt;
--&gt;

&lt;aop:config&gt;
    &lt;aop:advisor
        pointcut="execution(* net.sf.service.*.*(..))"
        advice-ref="txAdvice" /&gt;
&lt;/aop:config&gt;
&lt;tx:advice id="txAdvice"&gt;
    &lt;tx:attributes&gt;
        &lt;tx:method name="insert*" /&gt;
        &lt;tx:method name="update*" /&gt;
        &lt;tx:method name="*" propagation="REQUIRED"/&gt;
    &lt;/tx:attributes&gt;
&lt;/tx:advice&gt;
&lt;!--bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /--&gt;
&lt;bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
    &lt;property name="entityManagerFactory"
        ref="entityManagerFactory" /&gt;
&lt;/bean&gt;
&lt;bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
    destroy-method="close"&gt;
    &lt;property name="driver"&gt;&lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;&lt;/property&gt;
    &lt;property name="driverUrl"&gt;&lt;value&gt;jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))&lt;/value&gt;&lt;/property&gt;
    &lt;property name="user" value="test"&gt;&lt;/property&gt;
    &lt;property name="password" value="test"&gt;&lt;/property&gt;
    &lt;property name="alias" value="myblogdb"&gt;&lt;/property&gt;
    &lt;!--property name="houseKeepingSleepTime"&gt;&lt;value&gt;9000&lt;/value&gt;&lt;/property--&gt;
    &lt;property name="prototypeCount" value="5"&gt;&lt;/property&gt;
    &lt;property name="maximumConnectionCount" value="100"&gt;&lt;/property&gt;
    &lt;property name="minimumConnectionCount" value="10"&gt;&lt;/property&gt;
    &lt;property name="trace" value="true"&gt;&lt;/property&gt;
    &lt;property name="verbose" value="true"&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;property name="jpaVendorAdapter"&gt;
        &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
            &lt;property name="database" value="ORACLE" /&gt;
            &lt;property name="generateDdl" value="true" /&gt;
            &lt;property name="showSql" value="true" /&gt;
        &lt;/bean&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;context:component-scan base-package="net.sf"&gt;
    &lt;!--context:include-filter type="aspectj" expression="edu.jlu.fuliang.util..*"/--&gt;
    &lt;context:include-filter type="regex" expression="net\.sf\.service\..*"/&gt;
    &lt;context:include-filter type="regex" expression="net\.sf\.test\..*"/&gt;
    &lt;context:exclude-filter type="regex" expression="net\.sf\.action\..*"/&gt;
&lt;/context:component-scan&gt;

</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">
&lt;!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--&gt;
&lt;context:component-scan base-package="net.sf.action"/&gt;

&lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt;
&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/&gt;

&lt;!--  ③:对模型视图名称的解析,即在模型视图名称添加前后缀 --&gt;
&lt;bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"/&gt;

</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") // &lt;—— ①
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, 雪狐!";
}

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • apple_shan 2009-09-18 23:48
    已采纳

    我也遇到过这样的问题。。把json-lib-ext-spring的异常处理bean的bean配置项去掉就可以了。。

    打赏 评论
  • hua0424 2009-09-18 23:48

    JSP页面呢?

    打赏 评论
  • weixin_42306563 2009-09-18 23:48

    不知道楼主能不能把这个项目和我分享下呀、 我最近在学Spring MVC ... 我邮箱:mmzhengwei@live.cn 先谢谢啦、

    打赏 评论

相关推荐 更多相似问题