[struts的问题]一个类怎么样在jsp页面中显示

smalldeer 2004-08-27 10:37:29
需要显示一条id对应的记录
即有个链接:XXXXaction.do?id=10
点击链接后,把具体的id对应的记录显示在jsp页面上


我的想法(3个):
1.直接把类request set 到jsp中,用标签显示。

2.把类封装到一个list中,当然,list的size() =1 ,只容纳了一个类,在把list request set 到jsp中,用标签显示。

3.特别开个actionform,把类封装到actionform,则jsp可以直接调用了。


想请教合理的处理方法是怎么样的,谢谢。
...全文
269 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kui 2004-08-27
  • 打赏
  • 举报
回复
1、Struts自己带的例子是最典型的做法,当点击表格中一行的EDIT时,转到编辑页面编辑这条记录。

2、也可以看http://dev.csdn.net/user/kui中《如何在Struts 数据库应用程序中实现记录的删除、更新及链接》这个例子。
ccc426 2004-08-27
  • 打赏
  • 举报
回复
最简单的方法,还是将类当作一个javabean来调用
smalldeer 2004-08-27
  • 打赏
  • 举报
回复
谢谢大家,对我有帮助。

一直想找一个比较合理的方法。

不知道还有没有其它更多或更好的处理方法,谢谢
lguo_714 2004-08-27
  • 打赏
  • 举报
回复
up
YYSAM 2004-08-27
  • 打赏
  • 举报
回复
从数据库里面取出这条记录,封装到Bean里面。放到页面显示~
首先生成关于这条记录的简单Bean;
jb和Eclipse都有自动生成的功能;例如:

public class MagModuleBean implements Serializable {
private String moduleCode;
private String moduleName;
private String levelCode;
private String isShow;
public String getModuleCode() {
return moduleCode;
}
public void setModuleCode(String moduleCode) {
this.moduleCode = moduleCode;
}
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public String getLevelCode() {
return levelCode;
}
public void setLevelCode(String levelCode) {
this.levelCode = levelCode;
}
public String getIsShow() {
return isShow;
}
public void setIsShow(String isShow) {
this.isShow = isShow;
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}


如果你要显示多条记录,那把bean都放在ArrayList里面不是更好么~
解决的方法是使用类映射机制;


/////////////////////////////////////////////////////////////////////////////
//Function: 完成ResultSet对象向ArrayList对象为集合的对象的转化
//Para:sql,指定的查询Sql
//Para:className,Sql相对应得JavaBean/FormBean类的名字
//Return:以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集//合的className对象的转化
//////////////////////////////////////////////////////////////////////////////
public ArrayList Select(String sql,String className){
ArrayList paraList=new ArrayList();
try{
if (conn == null){
Connection();
}
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
String recordValue="";
Object c1=null;
paraList=new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()){
c1=Class.forName(className).newInstance();
for (int i=1; i<=columnCount; i++) {
if(rs.getString(rsmd.getColumnName(i))!=null){
recordValue=rs.getString(rsmd.getColumnName(i));
}else{
recordValue="";
}
Method
m=c1.getClass().getMethod(getSetMethodName(rsmd.getColumnName(i)),
new Class[]{recordValue.getClass()});
m.invoke (c1, new Object[]{recordValue});
}
paraList.add(c1);
}
}catch(SQLException ex){

}catch(ClassNotFoundException e){

}catch(NoSuchMethodException e) {

}catch(InvocationTargetException e){

}catch (IllegalAccessException e){

}catch(InstantiationException e){

} finaly{
closeConnection();
return paraList;
}


这个是个简单的例子;里面的用ArrayList封装可按照你的要求封装,但是有个问题也不好处理;
bean的属性名和数据库里面的字段名称对应的问题!这个还是要注意的。

如果你嫌上面写的麻烦,可以到Apache的网站上下载一个类包叫做dbutils 1.0.jar
他的BasicRowProcessor类有个方法,以下是他的简介:


toBeanList
public java.util.List toBeanList(java.sql.ResultSet rs,
java.lang.Class type)
throws java.sql.SQLException
Convert a ResultSet into a List of JavaBeans. This implementation uses reflection and BeanInfo classes to match column names to bean property names. Properties are matched to columns based on several factors:

The class has a writable property with the same name as a column. The name comparison is case insensitive.
The property's set method parameter type matches the column type. If the data types do not match, the setter will not be called.
Primitive bean properties are set to their defaults when SQL NULL is returned from the ResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set to null when SQL NULL is returned. This is the same behavior as the ResultSet get* methods.


Specified by:
toBeanList in interface RowProcessor
Returns:
A List of beans with the given type in the order they were returned by the ResultSet.
Throws:
java.sql.SQLException
See Also:
RowProcessor.toBeanList(java.sql.ResultSet, java.lang.Class)


看到是否是你想要的呢?其实他封装的操作和上面写的实例原理是一样的。
好了,希望对楼主有所帮助!
tomuno 2004-08-27
  • 打赏
  • 举报
回复
class Tomuno{
private String name="tomuno";
private String age="100";
private String id="8888";
public String getId(){
return this.id;}
public String getName(){
return this.name;}
public String getAge(){
return this.age;}
}
public class Smalldeer extends Action{
....
Tomuno tomuno=new Tomuno();
String id=(String)request.getParameter("id");
String mapping=null;
String name;
String age;

if(id.equals(tomuno.getId())){
mapping="success";
name = tomuno.getName();
age = tomuno.getAge();
request.getSession.setAttribute("name",name);
request.getSession.setAttribute("age",age);
}
else{mapping="fail";}

return actionmapping.findForward(mapping);
}

struts-config.xml
<action path="/tomuno" type="Tomuno">
<forward name="success" path="/smalldeer.jsp">
<forward name="fail" path="http://www.tom.com">
</action>

tomuno.jsp

tomuno.do?id=8888

smalldeer.jsp
<font color="blue">
My    name    is:
<%=session.getAttribute("name")%>
<br>
and  my  age   is;
<%=session.getAttribute("age")%>
</font>
tangbow 2004-08-27
  • 打赏
  • 举报
回复
作一个javabean来调用
cao8208 2004-08-27
  • 打赏
  • 举报
回复
哪个方便用哪个

不要拘泥于形式
pcdll 2004-08-27
  • 打赏
  • 举报
回复
第一个应该是最合理的,也是我一般采用的
第二个其实和第一个类似,还多了一个步骤
第三个是编辑/修改该信息时才使用的
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 用户账户控制(UAC)白名单的配置 Windows7环境 UAC(User Account Control,用户帐户控制)是由微软在Windows Vista版本推出的一项旨在增强系统安全性的创新技术,该技术强制要求用户在执行可能干扰计算机正常运作的操作或进行更改会波及其他用户设置的变动前,必须提供相应的权限或管理员密码进行验证。通过对这些操作启动前进行授权确认,UAC能够有效阻止恶意软件及间谍软件在未获授权的状态下于计算机内进行安装或实施修改。 自从Vista版本问世以来,微软便开始推行这一全新的安全机制,可视为对系统安全防护的显著提升。尽管UAC确实能够在一定程度上对某些非法程序起到防御作用,但与此同时,这一功能也给众多用户带来了诸多不便。 因此,许多用户开始探寻是否存在似于白名单的功能,以便将那些值得信赖的程序直接赋予运行权限。事实上,这功能确实存在,不过微软并未将其作为标准配置提供。 网络上关于此问题的绝大多数建议都是建议禁用UAC,这种说法显然缺乏针对性,因为若用户希望禁用此功能,本就不会提出相关疑问。 通过运用微软官方发布的Microsoft Application Compatibility Toolkit 5.6版本,可以将信任的程序纳入系统白名单范畴。 获取Application Compatibility Toolkit 安装程序成功后会出现三个可执行文件 以管理员身份启动Compatibility Administrator 在Custom DataBases部分创建新的数据库,并添加一个Application Fix(在下方空白处点击右键,选择...

67,535

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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