Type mismatch: cannot convert from Object to String

在jsp页面中获取从action中传入request中的值,报如下错误:

An error occurred at line: 28 in the jsp file: /homepage.jsp
Type mismatch: cannot convert from Object to String
25:   
26:   
27:     This is my homepage page. 
28: <%String ss = request.getAttribute("gender"); %> 29: gender: <% =ss%> 30: 31:

action中相关代码如下:

Map request = (Map)ActionContext.getContext().get("request");
Reader rea = (Reader)list.get(0);
request.put("gender", rea.getGender());

jsp中相关代码如下:

  
    This is my homepage page. 
<%String ss = request.getAttribute("gender"); %> gender: <%=ss%>

原因:
    request.getAttribute(“gender”);返回的是一个object对象,不能直接转换成String,需要强制转换,即采用:

<%String ss = (String)request.getAttribute("gender"); %>

替换掉jsp中的代码即可。

运行结果为:
运行结果图

你可能感兴趣的:(jsp,String,Object,request,mismatch,jsp)