servlet3.0+springmvc4零配置文件项目,返回json总是乱码,望大神指点~

火柴棍 发布于 2014/07/09 11:27
阅读 5K+
收藏 1

org.test.config java配置文件

spring注入配置

public class DefaultConfigration implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext context) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(SpringConfig.class);
        context.addListener(new ContextLoaderListener(rootContext));
        ServletRegistration.Dynamic dispatcher = context.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}


@Configuration
@ComponentScan(basePackages = "org.test.*")
public class SpringConfig {

    @Bean(destroyMethod = "close")
    public BasicDataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/vtools?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbc = new JdbcTemplate();
        jdbc.setDataSource(new SpringConfig().dataSource());
        return jdbc;
    }
    
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver inter = new InternalResourceViewResolver();
        inter.setPrefix("/WEB-INF/jsp/");
        inter.setSuffix(".jsp");
        return inter;
    }




servlet3.0配置


@WebFilter(urlPatterns = { "/*" }, initParams = { @WebInitParam(name = "encoding", value = "UTF-8"),
        @WebInitParam(name = "forceEncoding", value = "true") })
public class ServletFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("destory...");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chian) throws IOException,
            ServletException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("true");
        filter.setEncoding("UTF-8");
        filter.doFilter(request, response, chian);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("init...");
    }
}



controller 代码片段



@RequestMapping(value = "/jsonAction.json")
    @ResponseBody
    public String jsonAction() {
        return "真的会乱吗?会的...";
    }




就一个controller 返回是json对象,直接乱码。。。

不知道哪里配置错了。望有缘大虾来指点下。还需要什么代码,我继续贴~

加载中
0
len
len
先给个图看看你的乱码
0
赵建勇
len
len
回复 @赵建勇 : 调用responseBody应该是到不了 ViewResolver 里面的。所以这个配置无关
赵建勇
赵建勇
回复 @火柴棍 : 你看我不是给你的网页吗,里面有配置bean,你配置一个,试试
火柴棍
火柴棍
很不好意思的说。。我没看懂。。
0
kidbei
kidbei
@RequestMapping(value = "/jsonAction.json",produces="text/html;charset=utf-8")
0
火柴棍
火柴棍
@RequestMapping(value = "/jsonAction.json ",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public String jsonAction(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        return "真的会乱吗?会的...";
    }


也不行。。。

@kidbei



0
len
len
原因就是你的responseBody,这个时候你要用 new String("真的会乱吗?会的...".getByte('utf-8'))
0
张攀
张攀

修改tomcat配置文件中的sever.xml

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />



增加字符集编码,同时保证你jsp和java文件的编码是utf-8,还有jsp页面的编码也要是utf-8,全部统一的话就不会出现乱码。

0
len
len

想不用,编译环境要是Utf-8,一般Eclipse可以在工程里配置,另外改掉tomcat的配置,配置成UTF-8


0
Ida62
Ida62

其实,有一个笨办法,就是返回的只是一个整型的ID.

之后,前端跟剧这个ID,去输出相应的中文.

0
火柴棍
火柴棍
@RequestMapping(value = "/jsonAction.json ", produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public String jsonAction(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        String str = "";
        try {
            str = new String("真的会乱吗?会的...".getBytes(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

tomcat server.xml 的配置

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443"/>

我现在在想,我是不是撞邪了。。。你们说的方法,我都试过了。。。

@len @张攀

张攀
张攀
肯定是你不统一造成的,没有撞邪这种说法,只有方法不对。
返回顶部
顶部