标题:[求助][讨论]jsf的bean写业务逻辑合适么?
只看楼主
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
 问题点数:0 回复次数:3 
[求助][讨论]jsf的bean写业务逻辑合适么?

我在写jsf+hibernate的小例子时候
不知道将插入数据的代码写在什么地方
就写在了jsf的bean的方法里
再通过action="#{user.verify}"调用 来达到插入信息到数据库的操作
总感觉这样不是很好
应该怎么做?
初学jsf和hibernate 大家不要笑我啊
public String verify(){
User user=new User();
UserDAO udao=new UserDAO();
user.setUsername(username);
user.setPassword(password);
udao.save(user);
return "success";
}

搜索更多相关主题的帖子: jsf bean 数据库 逻辑 user 
2007-01-07 21:24
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
这有什么不可以的,,当然可以了,JSF的受管bean这样做可以把数据和行为封装在一起,挺好的啊

可惜不是你,陪我到最后
2007-01-07 22:42
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
得分:0 
我再想想

win32汇编
病毒 加密
目前兴趣所在
2007-01-07 22:43
renlong1333
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-5-6
得分:0 

一个关于课题Subject的JAVABean:
package com.jsfdemo.beans;

public class SubjectBean {
private String sb_id;
private String sb_name;
private String sb_desc;
private String t_name;
private String sb_state;
//--------------------------
public SubjectBean(){

}
//--------------------------
public String getSb_desc() {
return sb_desc;
}
public void setSb_desc(String sb_desc) {
this.sb_desc = sb_desc;
}
public String getSb_id() {
return sb_id;
}
public void setSb_id(String sb_id) {
this.sb_id = sb_id;
}
public String getSb_name() {
return sb_name;
}
public void setSb_name(String sb_name) {
this.sb_name = sb_name;
}
public String getSb_state() {
return sb_state;
}
public void setSb_state(String sb_state) {
this.sb_state = sb_state;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public SubjectBean(String sb_id,String sb_name,String sb_desc,String t_name,String sb_state ){
this.sb_id = sb_id;
this.sb_name = sb_name;
this.sb_desc = sb_desc;
this.t_name = t_name;
this.sb_state = sb_state;

}
}


一个数据库连接池的类:
package com.jsfdemo.beans;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.*;
import javax.sql.DataSource;
public class DBConnection {

static public Connection dataSource() {
try{
Context ctx = new InitialContext();
if(ctx==null)throw new NamingException ("no initial context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/dbtest");
if(ds==null) throw new NamingException ("no dataSource ");
// Connection con = ds.getConnection();
}catch (NamingException e){

//}catch (SQLException e){

}
return null;
}
}

一个操作数据库的JAVABean:
package com.jsfdemo.beans;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import javax.servlet.jsp.jstl.sql.Result;
import java.sql.SQLException;

public class SbDB {
// private Result AllSubject;
//********************************************
public void addSubject(SubjectBean subject) throws Exception,SQLException{
try{
Connection con =DBConnection.dataSource();
PreparedStatement pst = null;
//try{
pst = con.prepareStatement("insert into subjectInfo(sb_id,sb_name,sb_desc,t_id,sb_state) values(?,?,?,?,?)");
pst.setString(1, subject.getSb_id());
pst.setString(2, subject.getSb_name());
pst.setString(3, subject.getSb_desc());
pst.setString(4, subject.getT_name());
pst.setString(5, subject.getSb_state());
pst.executeUpdate();
//}catch(Exception e){
// e.printStackTrace();
//}
con.close();

}catch(Exception e){
e.printStackTrace();
}

}
}
// ********************************************

一个和JSP界面及JAVABean交互的类:
package com.jsfdemo.beans;
import javax.faces.event.ActionEvent;
import javax.servlet.jsp.jstl.sql.Result;


public class SbManager {

private SubjectBean subjectB = new SubjectBean();
private SbDB sbdb = new SbDB();
//*************************************

public SbManager(){

}
//*************************************

public SubjectBean getSubjectB() {
return subjectB;
}
public void setSubjectB(SubjectBean subjectB) {
this.subjectB = subjectB;
}
public SbDB getSbdb() {
return sbdb;
}

public void setSbdb(SbDB sbdb) {
this.sbdb = sbdb;
}
public String add(){
try{
sbdb.addSubject(subjectB);
}catch(Exception e){
e.printStackTrace();
}
return "addOK";
}

JSP界面:
<%@ page language = "java" contentType = "text/html;charset = gb2312"%>
<%@ taglib uri = "http://java.sun.com/jsf/core" prefix = "f"%>
<%@ taglib uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<html>
<center>
<f:view>
<h:form>

<head>
<title>
add the subject
</title>
</head>
<body>
<h:panelGrid columns = "2">
<h:outputText value = "subjectID"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_id}"></h:inputText>
<h:outputText value = "subjectName"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_name}"></h:inputText>
<h:outputText value = "subjectDesc"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_desc}"></h:inputText>
<h:outputText value = "teacher'sName"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.t_name}"></h:inputText>
<h:outputText value = "subject'sState"></h:outputText>
<h:inputText value = "#{SbManager.subjectB.sb_state}"></h:inputText>
<h:commandButton value = "submit..." action = "#{SbManager.add}"></h:commandButton>
</h:panelGrid>
<h:dataTable value = "#{SbManager.subjectB}" var = "sb" border = "1">
<h:column>
<f:facet name="header">
<h:outputText value = "subjectID"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_id}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subjectName"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subjectDesc"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_desc}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "teacher'sName"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.t_name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value = "subject'sState"></h:outputText>
</f:facet>
<h:outputText value = "#{sb.sb_state}"></h:outputText>
</h:column>
</h:dataTable>

</body>
</h:form>
</f:view>
</center>
</html>

其他配置正确(因为我做了测试),我先是把这些代码全部写在一个类里,能实现其功能,但是那样感觉太乱了,所以我把这些代码给分别写在了类里,结果是:
提交后,不显示错误,也不往数据库里插入信息,我已经是到了“山穷水尽”的地步了,实在解决不了这个问题,希望高手帮帮忙,谢谢啊!!!

2007-05-06 15:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-114776-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088334 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved