<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="jsp1_error.jsp" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<jsp:useBean id="jsp2BeanId" scope="session" class="untitled3.Jsp1Bean" />
<body bgcolor="#ffffff">
<form method="post">
Value of Bean property is :
<jsp:getProperty name="jsp2BeanId" property="sample" />
</form>
</body>
</html>
包:package untitled3;
public class Jsp1Bean {
  private String sample = "Start value aaa";
  //Access sample property
  public String getSample() {
    return sample;
  }
  //Access sample property
  public void setSample(String newValue) {
    if (newValue!=null) {
      sample = newValue;
    }
  }
}这是把<jsp:getProperty name="jsp2BeanId" property="sample" />的值让页面给显示出来。如何把它给一个变量呢?然后把这个变量再给到其它显示控件呢?

解决方案 »

  1.   

    <%
    String str=jsp2BeanId.getSample();
    %>
      

  2.   

    to:malligator(大螟) 原代码中
    <jsp:getProperty name="jsp2BeanId" property="sample" />
    应该如何理解呢?是相当于一个字符串显示在页面上面?
      

  3.   

    to:malligator(大螟)
    那如何把再把这个变量str的值给一个显示控件呢。比如有一控件:<input  type="text" id="t1" name="t1" value="000"/>以及页面代码:<jsp:useBean id="jsp2BeanId" scope="session" class="untitled3.Jsp1Bean" />
    在页面中起一个什么作用。这应该如何理解
      

  4.   

    id是定义的变量名,scope相当于一个变量的作用域,class就是你写的java类的路径
    那如何把再把这个变量str的值给一个显示控件呢?
    很简单:<input  type="text" id="t1" name="t1" 
    value="<jsp:getProperty name="jsp2BeanId" property="sample" />">
    就可以了
    不过建议lz用jstl 去实现这样的东东就很方便了
      

  5.   

    <input  type="text" id="t1" name="t1" 
    value="${jsp2BeanId.sample}">
      

  6.   

    对呀
    如果只是为了将它赋到控件里,用楼上的楼上的方法就可以了,没有必要弄个变量.
    若有其它处理:
    <input  type="text" id="t1" name="t1" 
    value=<%str%>/>楼上的方法跟楼上的楼上是等价的吧<jsp:useBean id="jsp2BeanId" scope="session" class="untitled3.Jsp1Bean" />
    相当于:
    java中的untitled3.Jsp1Bean jsp2BeanId=new untitled3.Jsp1Bean();
    //不过功能更强大一点?:它下面的<jsp:setProperty/>完成对类成员的赋值<jsp:getProperty name="jsp2BeanId" property="sample" />
    等同于:
    <%=jsp2BeanId.getSample()%>好久没用JSP,可能有些地方少个标点什么的~
      

  7.   

    <%
    String str=jsp2BeanId.getSample();
    %>
    <jsp:getProperty name="jsp2BeanId" property="sample" />
    等同于:
    <%=jsp2BeanId.getSample()%>
    楼上正解
    楼主,不好意思啊,你发消息时我在开会
      

  8.   

    建议楼主看看《jsp设计》第五版,很好的,有jstl的内容