jsp servlet javabean 实现MVC 奇怪问题.苦恼了许久:有知道问题原因的大虾帮帮忙!

whwsf2000 2009-11-26 01:27:44
主要原因:Jsp页面读不出从servlet中传过去的JavaBean的值
异常:
org.apache.jasper.JasperException: Cannot find any information on property 'mId' in a bean of type 'beans.Manager'

JavaBean: Manager(mId,mName,mPassword)



servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Connection conn = null;
Statement st = null;
ResultSet rs = null;

String url = "jdbc:mysql://localhost:3306/db_xbsk";
String username = "root";
String password = "";
String sql = "select * from manager";


try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,username,password);
st = conn.createStatement();

rs = st.executeQuery(sql);



Manager m = new Manager();

while(rs.next()){
m.setMId(rs.getInt("m_id"));
System.out.println("============到这了===========!");
m.setMName(rs.getString("m_name"));
m.setMPassword(rs.getString("m_password"));

System.out.println("============ID!"+m.getMId());
System.out.println("============Name!"+m.getMName());
System.out.println("============Password!"+ m.getMPassword());

HttpSession session = request.getSession();
session.setAttribute("mm", m);

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/display.jsp");
requestDispatcher.forward(request, response);
}
}catch (Exception e){
System.out.println(e.getMessage());
}
finally{
System.out.println("============资源释放!");
if(conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}
if(st != null){try {st.close();} catch (SQLException e) {e.printStackTrace();}}
if(rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}
}
}


前台Jsp代码:
<body>
<jsp:useBean id="mm" type="beans.Manager" scope="session" />
<jsp:getProperty name="mm" property="mId"/>
<jsp:getProperty name="mm" property="mName"/>
<jsp:getProperty name="mm" property="mPassword"/>

</body>

...全文
81 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
whwsf2000 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bayougeng 的回复:]
Bean中的方法名改成这样:
setmId
getmId
[/Quote]

这样可以。这样写出来了。谢谢.
谢谢大家
道光2008 2009-11-26
  • 打赏
  • 举报
回复
当前结贴
新问题,再开贴
chen7788 2009-11-26
  • 打赏
  • 举报
回复
<jsp:getProperty name="mm" property="mId"/>
试试这样<jsp:getProperty name="mm" property="${mm.mId}"/>
<jsp:getProperty name="mm" property="mName"/>
试试这样<jsp:getProperty name="mm" property="${mm.mName}"/>
<jsp:getProperty name="mm" property="mPassword"/>
试试这样<jsp:getProperty name="mm" property="${mm.mPassword}"/>
whwsf2000 2009-11-26
  • 打赏
  • 举报
回复
终于写出来了。
看来是<jsp:useBean 的问题。
这个问题是解决了。
新问题又来了。呵呵
谢谢美女程序员!
dawenwen1128 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 warison2008 的回复:]
还有检查下你的while(rs.next()){
这个东西循环了几次,如果是多次,就要把
HttpSession session = request.getSession();
session.setAttribute("mm", m);

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/display.jsp");
requestDispatcher.forward(request, response);
移到while(rs.next()){ }外面
[/Quote]


这个应该是正解
bayougeng 2009-11-26
  • 打赏
  • 举报
回复
Bean中的方法名改成这样:
setmId
getmId
道光2008 2009-11-26
  • 打赏
  • 举报
回复
还有检查下你的while(rs.next()){
这个东西循环了几次,如果是多次,就要把
HttpSession session = request.getSession();
session.setAttribute("mm", m);

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/display.jsp");
requestDispatcher.forward(request, response);
移到while(rs.next()){ }外面
道光2008 2009-11-26
  • 打赏
  • 举报
回复
这样你在jsp页面上直接写
<%
Manager mm = (Manager )session.getAttribute("mm");
System.out.println(mm.getMId());
%>
看看能获得结构不,如果能,说明你的 <jsp:useBean 用法有问题,如果不能,说明你的session根本没存在这个数据
whwsf2000 2009-11-26
  • 打赏
  • 举报
回复
Manager 里面定义的int类型。
这是servlet中的赋值语句。能赋值。
m.setMId(rs.getInt("m_id"));
道光2008 2009-11-26
  • 打赏
  • 举报
回复
Cannot find any information on property 'mId' in a bean of type 'beans.Manager'
?感觉是你Manager中的'mId' 定义有问题
whwsf2000 2009-11-26
  • 打赏
  • 举报
回复
改了.将跳转的语句放出来..运行..还是一样.jsp页面无法读JavaBean的值.
hyf0541210 2009-11-26
  • 打赏
  • 举报
回复
把那个设置属性的值也放在外面
while(rs.next()){
....
....
}

HttpSession session = request.getSession();
session.setAttribute("mm", m);
request.getRequestDispatcher("/display.jsp").requestDispatcher.forward(request, response);
hyf0541210 2009-11-26
  • 打赏
  • 举报
回复
跳转错了,跳转页面要放到循环外面 ,只能跳转一次
应改为
while(rs.next()){
....
....
HttpSession session = request.getSession();
session.setAttribute("mm", m);
}
request.getRequestDispatcher("/display.jsp").requestDispatcher.forward(request, response);
whwsf2000 2009-11-26
  • 打赏
  • 举报
回复
确定。控制台打印了:
============到这了===========!
============ID!2
============Name!旺旺小小酥
============Password!123321
道光2008 2009-11-26
  • 打赏
  • 举报
回复
while(rs.next()){
确认这句执行啦?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧