String date = "" + year + "-" + month+
"-" + datetime.getDate() + " "+00+":"+00+":"+00+"";
String sql = "select * from tb_news where IssDate between '"+date+"' and getDate()";
ResultSet rs = connection.executeQuery(sql); //执行查询的sql语句,返回到结果集中
try {
while (rs.next()) {
newsForm = new NewsActionForm();
newsForm.setNewID(Integer.valueOf(rs.getString(1)));
newsForm.setNewTitle(rs.getString(2));
newsForm.setNewContent(rs.getString(3));
newsForm.setNewsType(rs.getString(4));
newsForm.setNewsStyle(rs.getString(5));
newsForm.setNewIssueDate(rs.getString(6)); //按顺序取字段中的数据
list.add(newsForm);
} }
catch (NumberFormatException ex) {
}
catch (SQLException ex) {
}
return list;
}
}
在查询分析器中取系统时间的方法如图19所示
图19 利用getDate()方法取系统时间
2.服务层的新闻显示方法
接口类:
/************************ NewsFacade.java****************************/
public interface NewsFacade {
public List dateNews();
}
实现接口类:
/************************ NewsFacade.java****************************/
public class NewsFacadeImpl implements NewsFacade {
private NewsDao newsDao;
public NewsFacadeImpl() {
this.newsDao = new NewsDaoImpl(); //实现的方法
}
public List dateNews() {
return this.newsDao.dateNews(); //调用持久层的方法
}}
3.控制层的新闻显示方法
/************************ NewsFacade.java****************************/
package com.victor.frontStage.newsAction;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.victor.service.NewsFacadeImpl;
import com.victor.service.NewsFacade;
import java.util.List;
public class DateNews extends Action {
private NewsFacade newsFacade;
public DateNews() {
this.newsFacade = new NewsFacadeImpl();
}
public ActionForward perform(ActionMapping actionMapping,ActionForm actionForm,
HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
List list=this.newsFacade.dateNews(); //调用服务层的方法
httpServletRequest.setAttribute("listNew",list); //创建sql值对象
return actionMapping.findForward("dateNews");
}}
4.Struts-config.xml的配置
/****************************struts-config.xml*********************************/
<action name="newsActionForm" type="com.victor.webtier.newsAction.SelectOneNewsAction" validate="true" scope="request" path="/selectOneNewsAction">
<forward name="selectOneNewsAction" path="/jsp/managerBack/newsManager/updateNews.jsp" />
</action>
通过上述的方法,在jsp页面利用<%List list=(List)request.getAttribute("listNew");%>
取得对象的名称,在页面中使用即可。
6.2 输入新闻的关键字查找新闻
关键字查找新闻就是在文本框中输入新闻的相关内容,执行SQL语句,查找相关新闻信息,其中的下拉列表框是一个查询的动作,实现过程与前面的后台新闻相似,这里就不做介绍了。新闻查找运行结果如图20所示。
图20 关键字查询
关键字查找新闻控制层代码实现如下:
/****************************struts-config.xml*********************************/
package com.victor.frontStage.newsAction;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.victor.domain.NewsActionForm;
import com.victor.service.NewsFacade;
import com.victor.service.NewsFacadeImpl;
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页