/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.edu.bzu.news.struts.action;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import cn.edu.bzu.news.biz.NewsBiz;
import cn.edu.bzu.news.biz.UserBiz;
import cn.edu.bzu.news.entity.News;
import cn.edu.bzu.news.entity.NewsCategory;
import cn.edu.bzu.news.entity.User;
import cn.edu.bzu.news.struts.form.NewsForm;
public class NewsAction extends DispatchAction {
private NewsBiz newsBiz=null;
private UserBiz userBiz=null;
public ActionForward toIndex(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List newsList = newsBiz.getNewsList();
request.getSession().setAttribute("allNews", newsList);
return mapping.findForward("index");
}
public ActionForward toShow(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("newsId"));
News news = newsBiz.getNews(id);
newsBiz.addOneTimeClick(news);
request.getSession().setAttribute("oneNews", newsBiz.getNews(id));
return mapping.findForward("show");
}
public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int id = Integer.parseInt(request.getParameter("newsId"));
News news = newsBiz.getNews(id);
request.getSession().setAttribute("oneNews", news);
return mapping.findForward("update");
}
public ActionForward toAddNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return mapping.findForward("create");
}
public ActionForward toShowCategoryNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int categoryId=Integer.parseInt(request.getParameter("categorys"));
List newsList=newsBiz.getListByCategoryId(categoryId);
request.getSession().setAttribute("allNews", newsList);
return mapping.findForward("index");
}
public ActionForward doAddNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
NewsForm newsForm = (NewsForm) form;
// 从数据库中找到User
User user =userBiz.getUserByLoginName(newsForm.getUser().getLoginName());
// 从数据库中查找category
NewsCategory category=newsBiz.getCategoryById(newsForm.getCategory().getId());
News news=new News();
news.setTitle(newsForm.getNews().getTitle());
news.setPublisher(user);
news.setCategory(category);
news.setContent(newsForm.getNews().getContent());
news.setFirstDate(new Date());
news.setLastDate(new Date());
if(newsBiz.create(news)!=null){
request.getSession().setAttribute("newsList", newsBiz.getNewsList(newsForm.getUser().getLoginName()));
return mapping.findForward("mynews");
}
return null;
}
public ActionForward doUpdateNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String newsId = request.getParameter("newsId");
int id =Integer.parseInt(newsId);
NewsForm newsForm = (NewsForm) form;
// 从数据库中找到User
User user =userBiz.getUserByLoginName(newsForm.getUser().getLoginName());
// 从数据库中查找category
NewsCategory category=newsBiz.getCategoryById(newsForm.getCategory().getId());
News news=newsBiz.getNews(id);
news.setTitle(newsForm.getNews().getTitle());
news.setCategory(category);
news.setContent(newsForm.getNews().getContent());
news.setLastDate(new Date());
News news2 = newsBiz.updateNews(news);
if(news2!=null){
request.getSession().setAttribute("oneNews", news2);
return mapping.findForward("show");
}
return null;
}
public ActionForward doDelete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Integer newsId = Integer.parseInt(request.getParameter("newsId"));
if(newsBiz.deleteNews(newsId)){
return mapping.findForward("success");
}
return mapping.findForward("fail");
}
public ActionForward doSearchNews(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String title = request.getParameter("searchtitle");
List newsList=newsBiz.searchNewsList(title);
request.getSession().setAttribute("allNews", newsList);
return mapping.findForward("index");
}
public void setNewsBiz(NewsBiz newsBiz) {
this.newsBiz = newsBiz;
}
public NewsBiz getNewsBiz() {
return newsBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
public UserBiz getUserBiz() {
return userBiz;
}
}
- 1
- 2
- 3
- 4
前往页