利用Struts结合Jbuilder7、MySql建立Web站点(1)--连接数据库(修改)
作者:Sailing(蓝色虾)
本程序全部源代码:vod.zip
介绍:
关于Struts的介绍请参考:http://jakarta.apache.org
关于Struts的中文版用户指南:下载(我们要特别大富翁论坛的感谢曹晓刚先生的无私奉献,文章是他翻译的)
本文主要讨论如何利用Jbuilder7结合Struts建立一个后台数据库为MySQL的Web站点。
站点功能分析:(1)本网站为一个VOD点播网站,要能够满足用户的在线收看,下载电影文件,进行站内搜索
(2)网站内的所有影片分为足球射门、电影、搞笑片段、MTV四类别,并且在数据库中分别建立数据表
(3)首页出现以上四大类别的最新加入的前十个影片的资料,点击每个类别旁的“更多”链接出现该类别所有影片,按加入顺序倒排
(4)由于访问量大,使用数据库连接池。
(5)首页出现搜索栏,可以对以上四个类别中以影片名字作为关键字搜索
(6)首页出现专题推荐, 点击相应专题的名称,出现该专题所有影片
本篇文章的第一部分仅完成功能的(1)-(4)
准备:
首先我们从网上下载建立此Web站点需要的“包”,MySql的JDBC驱动程序可以在www.mysql.com下载,JDBC2.0的可选包随本程序的源
代码提供,Struts可以从http://jakarta.apache.org得到,Servlet包由Jbuilder7提供。
(1)打开Jbuilder7,建立一个新的工程(vod.jpx),将工程的默认编码改为GB2312,如下图:
(2)将获得包加入Jbuilder的工程需要包中,如图:
(3)建立一个Servlet和Jsp的空文件,分别将其执行,接着删除。(这样是为了JB自动建立相关的目录,并且获得Servlet的包)
(4)用winrar或者winzip解开下载的Struts中的webapps目录下的struts-example.war和struts-documentation.war(Struts的文档),
在struts-example.war解压后的\WEB-INF目录下找到struts.tld、struts-bean.tld、struts-form.tld、struts-html.tld、
struts-logic.tld、struts-template.tld文件,将其拷贝到在JB中建立的工程文件夹下的/WEB-INF目录下,然后在JB中将其加入工程,
以免在Rebuild时JB将其删除。如图:
(5)建立数据库,在MySql中建立一个名为gdvod的数据库,建立数据表需要的sql位于源代码目录src中的mysql.sql。
数据表如下:
表名:football
列名
类型
说明
id
varchar(10)
唯一的ID
footballName
varchar(50)
影片的名称
viewURL
varchar(255)
在线观看的网址
downURL
varchar(255)
下载的网址
clicks
int(5)
用户点击的次数
sort
varchar(20)
类别(默认:足球片段)
intro
text
简介
commend
int(5)
用户推荐次数
date
varchar(20)
加入日期
bad
int(5)
用户报告坏链接的次数
length
int(5)
影片的长度
其余的数据表于此类同,不同点在于fun、movie、mtv中的影片名称字段分别为funName、movieName、mtvName。
建立Bean
(1)与此对应,我们分别建立数据表相对于bean的映射。建立一个bean名为football,位于vod.mode包中
代码如下:
package vod.model;
import java.sql.SQLException;
import org.gjt.mm.mysql.jdbc2.ResultSet;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.Serializable;
public class Football implements Serializable {
private String id;
private String footballName;
private String viewURL;
private String downURL;
private int clicks;
private String sort;
private String intro;
private int commend;
private String date;
private int bad;
private int length;
//-------------------------------
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
//-------------------------------
public void setFootballName(String name){
this.footballName=name;
}
public String getFootballName(){
return footballName;
}
//-------------------------------
public void setViewURL(String url){
this.viewURL=url;
}
public String getViewURL(){
return viewURL;
}
//-------------------------------
public void setDownURL(String url){
this.downURL=url;
}
public String getDownURL(){
return downURL;
}
//-------------------------------
public void setClicks(int clicks){
this.clicks=clicks;
}
public int getClicks(){
return clicks;
}
//-------------------------------
public void setSort(String sort){
this.sort=sort;
}
public String getSort(){
return sort;
}
//-------------------------------
public void setIntro(String intro){
this.intro=intro;
}
public String getIntro(){
return intro;
}
//-------------------------------
public void setCommend(int commend){
this.commend=commend;
}
public int getCommend(){
return commend;
}
//-------------------------------
public void setDate(String date){
this.date=date;
}
public String getDate(){
return date;
}
//-------------------------------
public void setBad(int bad){
this.bad=bad;
}
public int getBad(){
return bad;
}
//-------------------------------
public void setLength(int length){
this.length=length;
}
public int getLength(){
return length;
}
public static Football load(ResultSet rs) throws SQLException{
Football football=new Football();
String strValue=null;
int intValue=0;
strValue=rs.getString("id");
if(strValue!=null)
football.setId(strValue);
strValue=rs.getString("footballName");
if(strValue!=null)
football.setFootballName(strValue);
strValue=rs.getString("viewURL");
if(strValue!=null)
football.setViewURL(strValue);
strValue=rs.getString("downURL");
if(strValue!=null)
football.setDownURL(strValue);
intValue=rs.getInt("clicks");
if(intValue > 0)
football.setClicks(intValue);
strValue=rs.getString("sort");
if(strValue!=null)
football.setSort(strValue);
/*
try{
Reader reader=rs.getCharacterStream("intro");
BufferedReader bReader=new BufferedReader(reader);
String inLine;
strValue="";
while((inLine=bReader.readLine())!= null){
strValue=strValue+inLine;
}
}catch(Exception e){e.printStackTrace();}
*/
strValue=rs.getString("intro");
if(strValue!=null)
football.setIntro(strValue);
intValue=rs.getInt("commend");
if(intValue > 0)
football.setCommend(intValue);
strValue=rs.getString("date");
if(strValue!=null)
football.setDate(strValue);
intValue=rs.getInt("bad");
if(intValue > 0)
football.setBad(intValue);
intValue=rs.getInt("length");
if(intValue > 0)
football.setLength(intValue);
return football;
}
}
public static Football load(ResultSet rs)方法作为一个该bean的静态方法,接受传来的一个ReseltSet对象,
并且在方法中生成了一个football的实例,将rs中的内容依次使用set方法赋给football,最后返回这个football实例
这样做的目的在下面的数据库连接段的代码将有说明,能够很好的将数据进行封装。
大家注意这里被注释掉的代码:
/*
try{
Reader reader=rs.getCharacterStream("intro");
BufferedReader bReader=new BufferedReader(reader);
String inLine;
strValue="";
while((inLine=bReader.readLine())!= null){
strValue=strValue+inLine;
}
}catch(Exception e){e.printStackTrace();}
*/
这段代码在程序运行的时候报错:说MySql的JDBC2没有实现rs.getCharacterStream("intro")这个方法,我感到很意外,
还是由于什么别的原因导致我不能使用这个方法,请朋友们指教。由于在数据库中使用了text类型,将有大量的文本使用
流读取将会提高程序的性能。
其它三个bean的代码如下:
package vod.model;
import org.gjt.mm.mysql.jdbc2.ResultSet;
import java.sql.SQLException;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.Serializable;
public class Fun implements Serializable {
private String id;
private String funName;
private String viewURL;
private String downURL;
private int clicks;
private String sort;
private String intro;
private int commend;
private String date;
private int bad;
private int length;
//-------------------------------
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
//-------------------------------
public void setFunName(String name){
this.funName=name;
}
public String getFunName(){
return funName;
}
//-------------------------------
public void setViewURL(String url){
this.viewURL=url;
}
public String getViewURL(){
return viewURL;
}
//-------------------------------
public void setDownURL(String url){
this.downURL=url;
}
public String getDownURL(){
return downURL;
}
//-------------------------------
public void setClicks(int clicks){
this.clicks=clicks;
}
public int getClicks(){
return clicks;
}
//-------------------------------
public void setSort(String sort){
this.sort=sort;
}
public String getSort(){
return sort;
}
//-------------------------------
public void setIntro(String intro){
this.intro=intro;
}
public String getIntro(){
return intro;
}
//-------------------------------
public void setCommend(int commend){
this.commend=commend;
}
public int getCommend(){
return commend;
}
//-------------------------------
public void setDate(String date){
this.date=date;
}
public String getDate(){
return date;
}
//-------------------------------
public void setBad(int bad){
this.bad=bad;
}
public int getBad(){
return bad;
}
//-------------------------------
public void setLength(int length){
this.length=length;
}
public int getLength(){
return length;
}
public static Fun load(ResultSet rs) throws SQLException{
Fun fun=new Fun();
String strValue=null;
int intValue=0;
strValue=rs.getString("id");
if(strValue!=null)
fun.setId(strValue);
strValue=rs.getString("funName");
if(strValue!=null)
fun.setFunName(strValue);
strValue=rs.getString("viewURL");
if(strValue!=null)
fun.setViewURL(strValue);
strValue=rs.getString("downURL");
if(strValue!=null)
fun.setDownURL(strValue);
intValue=rs.getInt("clicks");
if(intValue > 0)
fun.setClicks(intValue);
strValue=rs.getString("sort");
if(strValue!=null)
fun.setSort(strValue);
/*
try{
Reader reader=rs.getCharacterStream("intro");
BufferedReader bReader=new BufferedReader(reader);
String inLine;
strValue="";
while((inLine=bReader.readLine())!= null){
strValue=strValue+inLine;
}
}catch(Exception e){e.printStackTrace();}
*/
strValue=rs.getString("intro");
if(strValue!=null)
fun.setIntro(strValue);
intValue=rs.getInt("commend");
if(intValue > 0)
fun.setCommend(intValue);
strValue=rs.getString("date");
if(strValue!=null)
fun.setDate(strValue);
intValue=rs.getInt("bad");
if(intValue > 0)
fun.setBad(intValue);
intValue=rs.getInt("length");
if(intValue > 0)
fun.setLength(intValue);
return fun;
}
}
package vod.model;
import org.gjt.mm.mysql.jdbc2.ResultSet;
import java.sql.SQLException;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.Serializable;
public class Movie implements Serializable {
private String id;
private String movieName;
private String viewURL;
private String downURL;
private int clicks;
private String sort;
private String intro;
private int commend;
private String date;
private int bad;
private int length;
//-------------------------------
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
//-------------------------------
public void setMovieName(String name){
this.movieName=name;
}
public String getMovieName(){
return movieName;
}
//-------------------------------
public void setViewURL(String url){
this.viewURL=url;
}
public String getViewURL(){
return viewURL;
}
//-------------------------------
public void setDownURL(String url){
this.downURL=url;
}
public String getDownURL(){
return downURL;
}
//-------------------------------
public void setClicks(int clicks){
this.clicks=clicks;
}
public int getClicks(){
return clicks;
}
//-------------------------------
public void setSort(String sort){
this.sort=sort;
}
public String getSort(){
return sort;
}
//-------------------------------
public void setIntro(String intro){
this.intro=intro;
}
public String getIntro(){
return intro;
}
//-------------------------------
public void setCommend(int commend){
this.commend=commend;
}
public int getCommend(){
return commend;
}
//-------------------------------
public void setDate(String date){
this.date=date;
}
public String getDate(){
return date;
}
//-------------------------------
public void setBad(int bad){
this.bad=bad;
}
public int getBad(){
return bad;
}
//-------------------------------
public void setLength(int length){
this.length=length;
}
public int getLength(){
return length;
}
public static Movie load(ResultSet rs) throws SQLException{
Movie movie=new Movie();
String strValue=null;
int intValue=0;
strValue=rs.getString("id");
if(strValue!=null)
movie.setId(strValue);
strValue=rs.getString("movieName");
if(strValue!=null)
movie.setMovieName(strValue);
strValue=rs.getString("viewURL");
if(strValue!=null)
movie.setViewURL(strValue);
strValue=rs.getString("downURL");
if(strValue!=null)
movie.setDownURL(strValue);
intValue=rs.getInt("clicks");
if(intValue > 0)
movie.setClicks(intValue);
strValue=rs.getString("sort");
if(strValue!=null)
movie.setSort(strValue);
/*
try{
Reader reader=rs.getCharacterStream("intro");
BufferedReader bReader=new BufferedReader(reader);
String inLine;
strValue="";
while((inLine=bReader.readLine())!= null){
strValue=strValue+inLine;
}
}catch(Exception e){e.printStackTrace();}
*/
strValue=rs.getString("intro");
if(strValue!=null)
movie.setIntro(strValue);
intValue=rs.getInt("commend");
if(intValue > 0)
movie.setCommend(intValue);
strValue=rs.getString("date");
if(strValue!=null)
movie.setDate(strValue);
intValue=rs.getInt("bad");
if(intValue > 0)
movie.setBad(intValue);
intValue=rs.getInt("length");
if(intValue > 0)
movie.setLength(intValue);
return movie;
}
}
package vod.model;
import org.gjt.mm.mysql.jdbc2.ResultSet;
import java.sql.SQLException;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.Serializable;
public class Mtv implements Serializable {
private String id;
private String mtvName;
private String viewURL;
private String downURL;
private int clicks;
private String sort;
private String intro;
private int commend;
private String date;
private int bad;
private int length;
//-------------------------------
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
//-------------------------------
public void setMtvName(String name){
this.mtvName=name;
}
public String getMtvName(){
return mtvName;
}
//-------------------------------
public void setViewURL(String url){
this.viewURL=url;
}
public String getViewURL(){
return viewURL;
}
//-------------------------------
public void setDownURL(String url){
this.downURL=url;
}
public String getDownURL(){
return downURL;
}
//-------------------------------
public void setClicks(int clicks){
this.clicks=clicks;
}
public int getClicks(){
return clicks;
}
//-------------------------------
public void setSort(String sort){
this.sort=sort;
}
public String getSort(){
return sort;
}
//-------------------------------
public void setIntro(String intro){
this.intro=intro;
}
public String getIntro(){
return intro;
}
//-------------------------------
public void setCommend(int commend){
this.commend=commend;
}
public int getCommend(){
return commend;
}
//-------------------------------
public void setDate(String date){
this.date=date;
}
public String getDate(){
return date;
}
//-------------------------------
public void setBad(int bad){
this.bad=bad;
}
public int getBad(){
return bad;
}
//-------------------------------
public void setLength(int length){
this.length=length;
}
public int getLength(){
return length;
}
public static Mtv load(ResultSet rs) throws SQLException{
Mtv mtv=new Mtv();
String strValue=null;
int intValue=0;
strValue=rs.getString("id");
if(strValue!=null)
mtv.setId(strValue);
strValue=rs.getString("mtvName");
if(strValue!=null)
mtv.setMtvName(strValue);
strValue=rs.getString("viewURL");
if(strValue!=null)
mtv.setViewURL(strValue);
strValue=rs.getString("downURL");
if(strValue!=null)
mtv.setDownURL(strValue);
intValue=rs.getInt("clicks");
if(intValue > 0)
mtv.setClicks(intValue);
strValue=rs.getString("sort");
if(strValue!=null)
mtv.setSort(strValue);
/*
try{
Reader reader=rs.getCharacterStream("intro");
BufferedReader bReader=new BufferedReader(reader);
String inLine;
strValue="";
while((inLine=bReader.readLine())!= null){
strValue=strValue+inLine;
}
}catch(Exception e){e.printStackTrace();}
*/
strValue=rs.getString("intro");
if(strValue!=null)
mtv.setIntro(strValue);
intValue=rs.getInt("commend");
if(intValue > 0)
mtv.setCommend(intValue);
strValue=rs.getString("date");
if(strValue!=null)
mtv.setDate(strValue);
intValue=rs.getInt("bad");
if(intValue > 0)
mtv.setBad(intValue);
intValue=rs.getInt("length");
if(intValue > 0)
mtv.setLength(intValue);
return mtv;
}
}
(2)建立一个名为DisplayWelcome的类,这个类的任务是基于所设置的Connection分别将四大类别中按照加入顺序倒排前十
的条目放入四个LinkedList,并且返回所加入的LinkedList,以便于在Struts中的相关action中将其加入Session,便于在Jsp
页面中显示。注意这里的所用到刚刚建立的bean的load方法,将利用这个bean的静态方法返回同一个bean的实例,并且将其
加入到对应的LinkedList中。这样在LinkedList中保存的就是一个个具有不同数据的bean的实例,在jsp页面中可以很方便的
利用其get方法将相关的数据内容取出。
代码如下:
package vod.model;
import java.sql.Connection;
import org.gjt.mm.mysql.jdbc2.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.LinkedList;
public class DisplayWelcome {
Connection conn;
public void setConn(Connection conn){
this.conn=conn;
}
/**
* 取得按照加入顺序(依靠ID号)倒排的前十条数据
* @param conn
* @return
* @throws SQLException
*/
public LinkedList getFootballList() throws SQLException{
LinkedList footballList;
if(conn==null)
throw new SQLException("No Connection in DisplayWelcomeAction");
PreparedStatement pstmt = null;
ResultSet rs = null;
footballList=new LinkedList();
try {
// Prepare the query SQL
pstmt = conn.prepareStatement("select * from football order by id desc");
// Execute the query and copy the results
// to a List
rs = (org.gjt.mm.mysql.jdbc2.ResultSet)pstmt.executeQuery();
int i=0;
while (rs.next() && i < 10) {
footballList.add(Football.load(rs));
i++;
}
}
finally {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
}
return footballList;
}
/**
* 取得按照加入顺序(依靠ID号)倒排的前十条数据
* @param conn
* @return
* @throws SQLException
*/
public LinkedList getFunList() throws SQLException{
LinkedList funList;
if(conn==null)
throw new SQLException("No Connection in DisplayWelcomeAction");
PreparedStatement pstmt = null;
ResultSet rs = null;
funList=new LinkedList();
try {
// Prepare the query SQL
pstmt = conn.prepareStatement("select * from fun order by id desc");
// Execute the query and copy the results
// to a List
rs = (org.gjt.mm.mysql.jdbc2.ResultSet)pstmt.executeQuery();
int i=0;
while (rs.next() && i < 10) {
funList.add(Fun.load(rs));
i++;
}
}
finally {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
}
return funList;
}
/**
* 取得按照加入顺序(依靠ID号)倒排的前十条数据
* @param conn
* @return
* @throws SQLException
*/
public LinkedList getMovieList() throws SQLException{
LinkedList movieList;
if(conn==null)
throw new SQLException("No Connection in DisplayWelcomeAction");
PreparedStatement pstmt = null;
ResultSet rs = null;
movieList=new LinkedList();
try {
// Prepare the query SQL
pstmt = conn.prepareStatement("select * from movie order by id desc");
// Execute the query and copy the results
// to a List
rs = (org.gjt.mm.mysql.jdbc2.ResultSet)pstmt.executeQuery();
int i=0;
while (rs.next() && i < 10) {
movieList.add(Movie.load(rs));
i++;
}
}
finally {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
}
return movieList;
}
/**
* 取得按照加入顺序(依靠ID号)倒排的前十条数据
* @param conn
* @return
* @throws SQLException
*/
public LinkedList getMtvList() throws SQLException{
LinkedList mtvList;
if(conn==null)
throw new SQLException("No Connection in DisplayWelcomeAction");
PreparedStatement pstmt = null;
ResultSet rs = null;
mtvList=new LinkedList();
try {
// Prepare the query SQL
pstmt = conn.prepareStatement("select * from mtv order by id desc");
// Execute the query and copy the results
// to a List
rs = (org.gjt.mm.mysql.jdbc2.ResultSet)pstmt.executeQuery();
int i=0;
while (rs.next() && i < 10) {
mtvList.add(Mtv.load(rs));
i++;
}
}
finally {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
}
return mtvList;
}
}
(3)建立Action,在Struts中的页面的定位都是通过action来完成,action控制着网站程序因该调用哪一个BuinessBean来处理
用户的请求。这里建立一个名为:DisplayWelcomeAction的Action的Action,这个action的任务是接受用户的请求,接着调用
Struts自带的数据库连接池取得一个Connection,然后将这个数据库连接通过setConn()设置DisplayWelcome的一个实例,做后
利用相应的getXXXList()方法取得对应的LinkedList,将其加入session,以方便内容载jsp页面的显示。
代码如下:
package vod.controller;
import vod.model.DisplayWelcome;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DisplayWelcomeAction extends Action {
Connection connection;
LinkedList footballList;
LinkedList funList;
LinkedList movieList;
LinkedList mtvList;
/**
* 通过vod.model.DisplayWeclome各个类别0取得排名前十的条目,将其放入request
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
* @throws ServletException
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
ActionErrors errors = new ActionErrors();
try {
DataSource dataSource =servlet.findDataSource(null);
connection =dataSource.getConnection();
footballList=new LinkedList();
funList=new LinkedList();
movieList=new LinkedList();
mtvList=new LinkedList();
DisplayWelcome displaywelcome=new DisplayWelcome();
displaywelcome.setConn(connection);
footballList=displaywelcome.getFootballList();
funList=displaywelcome.getFunList();
movieList=displaywelcome.getMovieList();
mtvList=displaywelcome.getMtvList();
if (footballList==null) {
saveErrors(request, errors);
return (new ActionForward("No footballList in vod.controller.DisplayWelcomeAction"));
}
if (funList==null) {
saveErrors(request, errors);
return (new ActionForward("No funList in vod.controller.DisplayWelcomeAction"));
}
if (movieList==null) {
saveErrors(request, errors);
return (new ActionForward("No movieList in vod.controller.DisplayWelcomeAction"));
}
if (mtvList==null) {
saveErrors(request, errors);
return (new ActionForward("No mtvList in vod.controller.DisplayWelcomeAction"));
}
HttpSession session = request.getSession();
session.setAttribute("footballTop",footballList);
session.setAttribute("funTop",funList);
session.setAttribute("movieTop",movieList);
session.setAttribute("mtvTop",mtvList);
//do what you wish with myConnection
} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//enclose this in a finally block to make
//sure the connection is closed
if(connection!=null)
try {
connection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}
return (mapping.findForward("success"));
}
}
配置Struts:
(1)此时bean和action都已经完成了,下面我们将配置Struts的相关xml文件了,首先在JB中打开web.xml文件,将以下内容
加入web.xml文件:
<!--指明action类所在的位置 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<!--指明ApplicationResources文件所在的位置 -->
<param-name>application</param-name>
<param-value>vod.ApplicationResources</param-value>
</init-param>
<init-param>
<!--指明struts-config.xml文件所在的位置 -->
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!--经所有.do结尾的请求映射倒第一行指定的名为action的org.apache.struts.action.ActionServlet -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--指明首页文件的名称 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--指明/struts-bean.tld文件的位置 -->
<taglib>
<!--在网页中引用这个标签的URI -->
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<!--指明struts-html.tld文件的位置 -->
<taglib>
<!--在网页中引用这个标签的URI -->
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<!--指明struts-logic.tld文件的位置 -->
<taglib>
<!--在网页中引用这个标签的URI -->
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
(2)在工程目录的WEB-INF/目录中建立一个名为struts-config.xml的文件,内容如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== 数据库连接池的相关设置=============================== -->
<data-sources>
<data-source>
<set-property property="autoCommit"
value="false"/>
<set-property property="description"
value="Data Source Configuration"/>
<!-- 数据库的JDBC驱动程序地址 -->
<set-property property="driverClass"
value="org.gjt.mm.mysql.Driver"/>
<!-- 最大数据库连接数 -->
<set-property property="maxCount"
value="200"/>
<!-- 最小数据库连接数 -->
<set-property property="minCount"
value="20"/>
<set-property property="password"
value="你的数据库密码"/>
<set-property property="url"
value="jdbc:mysql://localhost/gdmovie"/>
<set-property property="user"
value="你的数据库的用户名"/>
</data-source>
</data-sources>
<!-- ========== Global Forward Definitions ============================== -->
<!--全局的跳转,将在本程序的index.jsp中使用-->
<global-forwards>
<forward name="welcome" path="/welcome.do"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<!-- Enter into Welcome Page -->
<!--
path:请求的URL
type:action所处的位置
forward:如果action执行成功,那么跳转到welcome页
-->
<action path="/welcome"
type="vod.controller.DisplayWelcomeAction">
<forward name="success" path="/welcome.jsp"/>
</action>
</action-mappings>
</struts-config>
(3)在工程目录的WEB-INF/目录中建立一个名为action.xml的文件,内容如下:
<!-- Action Mappings for the STRUTS Example Application -->
<action-mappings>
<!-- Global Forward Declarations -->
<forward name="welcome" path="/welcome.do"/>
<!-- Process a user logon -->
<action path="/welcome"
actionClass="vod.controller.DisplayWelcomeAction">
<forward name="success" path="/welcome.jsp"/>
</action>
</action-mappings>
action.xml任务是指明在action内部的跳转,关于这方面我一直觉得这和struts-config.xml中的内容产生重复,不怎么
理解这个action.xml文件存在的意义,我的领悟能力有限,请大家指教。
建立Jsp页面
(1)建立首页:由于这个页面第一个出现在用户面前的页面就含有数据库的读取,但是由于在Struts中所有的数据库的
读取必须通过action调用相关的bean中的功能实现,所以这里的首页起的作用就是中转一下用户的请求,将其跳转到
welcome.jsp,并且调转是由一个action负责,在跳转的同时在这个action中调用相关的bean读取数据库。
代码如下:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:forward name="welcome"/>
这里的<logic:forward name="welcome"/>标签其实调用了<forward name="welcome" path="/welcome.do"/>,并且
所有*.do结尾的请求映射到了action上,同时由于设置了
<action path="/welcome"
type="vod.controller.DisplayWelcomeAction">
<forward name="success" path="/welcome.jsp"/>
</action>
DisplayWelcomeAction接受了这个请求,在数据库中将相关的内容读取,放入对应的LinkedList中,并且放入了session。
(2)建立welcome.jsp,这个页面就是将放入session的LinkedList读出,进行iterate,将其呈现在网页上,注意这里标签
的使用,代码如下:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<title>
welcome
</title>
</head>
<body>
<table width="694" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="70"><div align="center">id</div></td>
<td width="110"><div align="center">footname</div></td>
<td width="82"><div align="center">viewURL</div></td>
<td width="92"><div align="center">downURL</div></td>
<td width="48"><div align="center">clicks</div></td>
<td width="41"><div align="center">sort</div></td>
<td width="40"><div align="center">intro</div></td>
<td width="66"><div align="center">commend</div></td>
<td width="61"><div align="center">date</div></td>
<td width="33"><div align="center">bad</div></td>
<td width="51"><div align="center">length</div></td>
</tr>
<%/*
java.util.LinkedList link=new java.util.LinkedList();
link=(java.util.LinkedList)session.getAttribute("footballTop");
if(link!=null)
out.print("true");
else
out.print("false");
*/%>
<logic:iterate id="football" name="footballTop">
<tr>
<td><div align="center"><bean:write name="football" property="id"/></div></td>
<td><div align="center"><bean:write name="football" property="footballName"/></div></td>
<td><div align="center"><bean:write name="football" property="viewURL"/></div></td>
<td><div align="center"><bean:write name="football" property="downURL"/></div></td>
<td><div align="center"><bean:write name="football" property="clicks"/></div></td>
<td><div align="center"><bean:write name="football" property="sort"/></div></td>
<td><div align="center"><bean:write name="football" property="intro"/></div></td>
<td><div align="center"><bean:write name="football" property="commend"/></div></td>
<td><div align="center"><bean:write name="football" property="date"/></div></td>
<td><div align="center"><bean:write name="football" property="bad"/></div></td>
<td><div align="center"><bean:write name="football" property="length"/></div></td>
</tr>
</logic:iterate>
</table>
<br>
<table width="694" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="70"><div align="center">id</div></td>
<td width="110"><div align="center">funname</div></td>
<td width="82"><div align="center">viewURL</div></td>
<td width="92"><div align="center">downURL</div></td>
<td width="48"><div align="center">clicks</div></td>
<td width="41"><div align="center">sort</div></td>
<td width="40"><div align="center">intro</div></td>
<td width="66"><div align="center">commend</div></td>
<td width="61"><div align="center">date</div></td>
<td width="33"><div align="center">bad</div></td>
<td width="51"><div align="center">length</div></td>
</tr>
<logic:iterate id="fun" name="funTop">
<tr>
<td><div align="center"><bean:write name="fun" property="id"/></div></td>
<td><div align="center"><bean:write name="fun" property="funName"/></div></td>
<td><div align="center"><bean:write name="fun" property="viewURL"/></div></td>
<td><div align="center"><bean:write name="fun" property="downURL"/></div></td>
<td><div align="center"><bean:write name="fun" property="clicks"/></div></td>
<td><div align="center"><bean:write name="fun" property="sort"/></div></td>
<td><div align="center"><bean:write name="fun" property="intro"/></div></td>
<td><div align="center"><bean:write name="fun" property="commend"/></div></td>
<td><div align="center"><bean:write name="fun" property="date"/></div></td>
<td><div align="center"><bean:write name="fun" property="bad"/></div></td>
<td><div align="center"><bean:write name="fun" property="length"/></div></td>
</tr>
</logic:iterate>
</table>
<br>
<table width="694" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="70"><div align="center">id</div></td>
<td width="110"><div align="center">moviename</div></td>
<td width="82"><div align="center">viewURL</div></td>
<td width="92"><div align="center">downURL</div></td>
<td width="48"><div align="center">clicks</div></td>
<td width="41"><div align="center">sort</div></td>
<td width="40"><div align="center">intro</div></td>
<td width="66"><div align="center">commend</div></td>
<td width="61"><div align="center">date</div></td>
<td width="33"><div align="center">bad</div></td>
<td width="51"><div align="center">length</div></td>
</tr>
<logic:iterate id="movie" name="movieTop">
<tr>
<td><div align="center"><bean:write name="movie" property="id"/></div></td>
<td><div align="center"><bean:write name="movie" property="movieName"/></div></td>
<td><div align="center"><bean:write name="movie" property="viewURL"/></div></td>
<td><div align="center"><bean:write name="movie" property="downURL"/></div></td>
<td><div align="center"><bean:write name="movie" property="clicks"/></div></td>
<td><div align="center"><bean:write name="movie" property="sort"/></div></td>
<td><div align="center"><bean:write name="movie" property="intro"/></div></td>
<td><div align="center"><bean:write name="movie" property="commend"/></div></td>
<td><div align="center"><bean:write name="movie" property="date"/></div></td>
<td><div align="center"><bean:write name="movie" property="bad"/></div></td>
<td><div align="center"><bean:write name="movie" property="length"/></div></td>
</tr>
</logic:iterate>
</table>
<br>
<table width="694" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="70"><div align="center">id</div></td>
<td width="110"><div align="center">mtvname</div></td>
<td width="82"><div align="center">viewURL</div></td>
<td width="92"><div align="center">downURL</div></td>
<td width="48"><div align="center">clicks</div></td>
<td width="41"><div align="center">sort</div></td>
<td width="40"><div align="center">intro</div></td>
<td width="66"><div align="center">commend</div></td>
<td width="61"><div align="center">date</div></td>
<td width="33"><div align="center">bad</div></td>
<td width="51"><div align="center">length</div></td>
</tr>
<logic:iterate id="mtv" name="mtvTop">
<tr>
<td><div align="center"><bean:write name="mtv" property="id"/></div></td>
<td><div align="center"><bean:write name="mtv" property="mtvName"/></div></td>
<td><div align="center"><bean:write name="mtv" property="viewURL"/></div></td>
<td><div align="center"><bean:write name="mtv" property="downURL"/></div></td>
<td><div align="center"><bean:write name="mtv" property="clicks"/></div></td>
<td><div align="center"><bean:write name="mtv" property="sort"/></div></td>
<td><div align="center"><bean:write name="mtv" property="intro"/></div></td>
<td><div align="center"><bean:write name="mtv" property="commend"/></div></td>
<td><div align="center"><bean:write name="mtv" property="date"/></div></td>
<td><div align="center"><bean:write name="mtv" property="bad"/></div></td>
<td><div align="center"><bean:write name="mtv" property="length"/></div></td>
</tr>
</logic:iterate>
</table>
</body>
</html>
现在以footballTop进行iterate标签的说明,iterate标签的语法如下:
<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>
网页中使用的标签:
<logic:iterate id="football" name="footballTop">
<tr>
<td><div align="center"><bean:write name="football" property="id"/></div></td>
</tr>
</logic:iterate>
这里的footballTop是我在DisplayWelcomeAction中放入session的footballList的属性名字。
相关代码--DisplayWelcomeAction:session.setAttribute("footballTop",footballList);
id="football",是指放入这个LinkedList中的bean的名字。
相关代码--DisplayWelcome:footballList.add(Football.load(rs));
Football:return football;
<bean:write name="football" property="id"/>
property:是放了LinkedList中的football中的id的内容,但是必须要在football中有相应的getId()方法才能如此
调用。
此时在中Run index.jsp便可以看见结果了。
结尾:
本程序全部源代码:vod.zip
我写着篇文章的目的只由于当时在网上只能找到很少的关于Struts的中文资料,特别来火,干脆自己就写了一篇。
真心希望网上关于Java方面的中文资料能够越来越多。
在使用Tomacat是产生了 在JB7中配合tomact+mysql使用连接池的问题,请大家帮忙解答,多谢了。
关于作者:
Sailing,就读于南京工业大学工商管理专业,爱好Java编程,熟悉网站制作,真诚希望与大家进行交流。
工作室:Mars Studio