使用struts的标签时怎么才能把从数据库中查

使用struts的标签时怎么才能把从数据库中查,第1张

先在action里定义一个集合并通过业务程的方法取得数据,如我下面的channelList;在页面需通过select的name=""来获得action中定义的集合,再在option里指定value,value为集合的属性:

<select name="channelList" id="channel_${sta.index}" onchange="getChannelCode(this)" style="width:130font-size:11px" ${allot.channel==null?'':'disabled' }>

<option value="${allot.brand.brd_code}">${allot.channel.com_desc}</option>

<s:if test="#allot.brand!=null">

<s:if test="#allot.channel==null">

<s:iterator value="allChannel">

<option value="${com_code }">${com_desc }</option>

</s:iterator>

</s:if>

</s:if>

</select>

DAO 的select://自己对照去改参数!我查询的表里面有4个字段!

public static String login(String id,String word){

Connection conn = null

Statement stmt = null

ResultSet rs = null

String power=""

try{

conn = min.getConnection()

stmt = conn.createStatement()

rs = stmt.executeQuery( "SELECT * FROM roudian WHERE userid='"+id+"' AND password='"+word+"'")

while(rs.next()){

power=rs.getString(4)

}

}catch(Exception e){

e.printStackTrace()

}finally{

min.closeConnection(conn)

}

return power

}

Action:

if(request.getParameter("method").equals("login")){

String id = request.getParameter("id")

String word = request.getParameter("word")

String power=DeptDao.login(id,word)

if(power.equals("1")||power.equals("2")){

forword="adminframe"

session.setAttribute("power",power)

}else{

message="用户名或密码错误!"

forword="select"

}

}

if(request.getParameter("method").equals("power")){

String power=session.getAttribute("power").toString()

if(power.equals("1")){

forword="dept"

session.setAttribute("power",power)

}else{

message = "失败"

forword="select"

}

}

先写的DAO:public List<FileModel>findAll()

{

Connection con = null

PreparedStatement ps = null

FileModel file = null

ResultSet rs = null

List<FileModel>set = null

try

{

con = DBconnection.getConnection()

String sql = "select * from file

ps = con.prepareStatement(sql)

set = new LinkedList<FileModel>()

rs = ps.executeQuery()

while(rs.next())

{

file = new FileModel()

file.setFilepath(rs.getString(1))

file.setFiletime(rs.getTimestamp(2))

file.setFileintroduce(rs.getString(3))

file.setFilediscuss(rs.getString(4))

file.setFilescore(rs.getFloat(5))

file.setFiletype(rs.getString(6))

file.setDirection(rs.getString(7))

file.setFileid(rs.getInt(8))

file.setFilename(rs.getString(9))

set.add(file)

}

}

catch(SQLException e)

{

throw new RuntimeException(e.getMessage(),e)

}

finally

{

DBconnection.free(rs, ps, con)

}

return set

}

在action中调用DAO:

public class FindAction extends ActionSupport {

private Dao dao = new Dao()

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

LinkedList<FileModel>modelSet = (LinkedList<FileModel>) dao.findAll()

if (modelSet!=null){

System.out.println(modelSet)

ActionContext.getContext().getSession().put("msg", modelSet)

return SUCCESS}

else

return ERROR

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6673406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存