2 yjwzsrzj YJWZSRZJ 于 2017.09.03 13:28 提问

map 返回值为空 不知道为啥

//查询表中的所有的数据
@Action(value="user",results={@Result(name="succes", location="/WEB-INF/jsp/user.jsp")})
public String userall(){
Session session = sessionFactory.openSession();
Query query=session.createQuery("from user");
List userlist=query.list();
for (UserEntity userEntity : userlist) {
listuser.add(userEntity);
}
session.close();
return "succes";

}

@Action(value="json")
public Map<String, Object> json(){  
    HttpServletResponse response = ServletActionContext.getResponse();
    map=new HashMap<String, Object>();
        int total=listuser.size();
        map.put("total",total);
        map.put("rows", listuser);

        String data = JSON.toJSONString(map);
        try {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out;
            out = response.getWriter();
            out.write(data.toString());
            System.out.println(data);
            out.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
}
    ![f12调试后没有值图片说明](http://img.ask.csdn.net/upload/201709/03/1504416375_892008.png)

    java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
at com.opensymphony.xwork2.DefaultActionInvocation.saveResult(DefaultActionInvocation.java:483)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:284)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:253)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:177)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:260)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:69)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.doIntercept(ConversionErrorInterceptor.java:139)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:192)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:69)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:121)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:88)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:245)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:99)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:139)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:155)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:174)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:151)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:171)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:195)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
at org.apache.struts2.factory.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:113)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

2个回答

Small_Mouse0
Small_Mouse0   Ds   Rxr 2017.09.03 14:13

public Map json()这个方法的return map;,是空??

java.util.HashMap cannot be cast to java.lang.String
HashMap不能强转为String,,看看这个类【DefaultActionInvocation.java】

YJWZSRZJ
YJWZSRZJ 值出来了,可是他还是报这个错,并且我一刷新,值会在原来的基础上在把值添加上去,和数据库的值是重复的
3 个月之前 回复
Small_Mouse0
Small_Mouse0 回复一只会飞的白菜: 题主这个是Struts2吧,,我不是很熟,,根据报错猜一下,,可能是这里返回值有问题,public Map<String, Object> json(),,都已经把map转json了,,直接返回String data = JSON.toJSONString(map);这个data可以不可以??试试
3 个月之前 回复
YJWZSRZJ
YJWZSRZJ map 有值了还是报哪个错报java.util.HashMap cannot be cast to java.lang.String,我输出了下{"total":1,"rows":[{"listroomnumber":[],"ucard":"456789795645","ucount":1,"ugender":1,"uid":1,"uname":"哈哈哈","uphone":"123465"}]}
3 个月之前 回复
YJWZSRZJ
YJWZSRZJ {"total":0,"rows":[]}输出了下 是个个
3 个月之前 回复
YJWZSRZJ
YJWZSRZJ 是的,返回的空,
3 个月之前 回复
linqin107412
linqin107412   2017.09.03 15:12

注意看你定义的map类型, Map map = new HashMap
map.put("rows", listuser);你这里放了一个集合listuser,然后你直接JSON.toJSONString(map);那就说明你这个toJSONString方法里面有问题,没有转好

YJWZSRZJ
YJWZSRZJ 值出来了,可是他还是报这个错,并且我一刷新,值会在原来的基础上在把值添加上去,和数据库的值是重复的
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片