cxshun
cxshun
2012-09-25 11:30

spring responseBody返回xml在IE下有问题

  • spring
  • 企业应用

我使用spring的responseBody返回xml,在chrome,firefox下均没问题,但在IE下会出现应用程序出错的问题。
我的代码如下:
UserController:
[code="java"]
package com.spring;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

@RequestMapping("/user")
public @ResponseBody User getUser(){
    User user = new User();
    user.setName("shun");
    user.setPassword("123123");
    return user;
}

}

@XmlRootElement(name="user")
class User {
private String name;
private String password;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

[/code]

web.xml
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>


spring
org.springframework.web.servlet.DispatcherServlet
1


spring
/


index.jsp


[/code]

spring-servlet.xml
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring" />

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml" />
            <entry key="json" value="application/json" />
            <entry key="jsonp" value="application/javascript" />
        </map>
    </property>
    <property name="defaultContentType" value="text/html" />
    <property name="ignoreAcceptHeader" value="true" />
    <property name="favorPathExtension" value="true" />
    <property name="favorParameter" value="true" />
    <property name="parameterName" value="return_fmt"></property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller">
                    <ref bean="castorMarshaller"/>
                </property>
            </bean>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />

[/code]

当我用其他浏览器的时候,返回正常,如下:

[img]http://dl.iteye.com/upload/attachment/0074/3130/9124add8-e4e1-31a8-b13b-f6ec0aaf059c.png[/img]

而用IE的时候,返回如下:

[img]http://dl.iteye.com/upload/attachment/0074/3125/d7768747-ca04-3d0d-8916-905b2da1ddec.png[/img]

我在项目中有打印执行方法的次数,发现在IE中会进行两次访问,之后就出现如图的错误,猜想可能是进行了循环的访问。

有哪位朋友遇到过这样的问题,或知道出错在哪里,忘告知,多谢。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐