📄 articleservlet.java
字号:
if(info==null||info.equals("")){
mark=false;
messages+="<li>请输入 <b>文章描述!</b></li>";
}
if(content==null||content.equals("")){
mark=false;
messages+="<li>请输入 <b>文章内容!</b></li>";
}
if(mark){
title=MyTools.toChinese(title);
content=MyTools.toChinese(content);
if(title.length()>20){
mark=false;
messages+="<li><b>文章标题</b> 最多允许输入20个字符!</li>";
}
if(content.length()>4000){
mark=false;
messages+="<li><b>文章内容</b> 最多允许输入4000个字符!</li>";
}
}
request.setAttribute("messages",messages);
return mark;
}
/**
* @功能 后台-增加文章
*/
public void addArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String messages = "";
String href="";
String forward="";
boolean flag=validateArticle(request,response);
if(flag){
ArticleBean articleBean = new ArticleBean();
articleBean.setTypeId(MyTools.strToint(request.getParameter("typeId")));
articleBean.setTitle(MyTools.toChinese(request.getParameter("title")));
articleBean.setContent(MyTools.changeHTML(MyTools.toChinese(request.getParameter("content"))));
articleBean.setSdTime(MyTools.changeTime(new Date()));
articleBean.setCreate(MyTools.toChinese(request.getParameter("create")));
articleBean.setInfo(MyTools.toChinese(request.getParameter("info")));
articleBean.setCount(0);
ArticleDao articleDao = new ArticleDao();
boolean mark=articleDao.operationArticle("add", articleBean);
if(mark) {
messages = "<li>发表文章成功!</li>";
href="<a href='admin/article/ArticleAdd.jsp'>[继续发表]</a>";
forward="/admin/success.jsp";
}
else{
messages="<li>发表文章失败!</li>";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
forward="/admin/error.jsp";
}
request.setAttribute("messages",messages);
request.setAttribute("href",href);
}
else{
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
forward="/admin/error.jsp";
request.setAttribute("href",href);
}
RequestDispatcher rd = request.getRequestDispatcher(forward);
rd.forward(request, response);
}
/**
* @功能 实现后台文章管理中的文章浏览功能
*/
public void adminSelectList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
ArticleDao articleDao = new ArticleDao();
String strId=request.getParameter("typeId");
if(strId==null||strId.equals(""))
strId="-1";
int typeId=Integer.parseInt(strId);
session.setAttribute("showTypeId",typeId);
List articleList=articleDao.queryArticle(typeId,"all");
request.setAttribute("articleList",articleList);
RequestDispatcher rd=request.getRequestDispatcher("/admin/article/ArticleList.jsp");
rd.forward(request,response);
}
public void adminSelectSingle(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
int id=MyTools.strToint(request.getParameter("id"));
ArticleDao articleDao = new ArticleDao();
ArticleBean articleBean=articleDao.queryArticleSingle(id); //查询指定文章的详细内容
request.setAttribute("articleSingle",articleBean);
RequestDispatcher rd=request.getRequestDispatcher("/admin/article/ArticleSingle.jsp");
rd.forward(request,response);
}
/**
* @功能 从数据表中获取某类别下的所有文章
*/
public void selectArticle(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ArticleDao articleDao = new ArticleDao();
String strId=request.getParameter("typeId");
if(strId==null||strId.equals(""))
strId="-1";
int typeId=Integer.parseInt(strId);
List articleList=articleDao.queryArticle(typeId,"all");
request.setAttribute("articleList",articleList);
RequestDispatcher rd=request.getRequestDispatcher("/front/article/ArticleList.jsp");
rd.forward(request,response);
}
public boolean validateType(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
boolean mark=true;
String messages="";
String typeName=request.getParameter("typeName");
String typeInfo=request.getParameter("typeInfo");
if(typeName==null||typeName.equals("")){
mark=false;
messages+="<li>请输入 <b>类别名称!</b></li>";
}
if(typeInfo==null||typeInfo.equals("")){
mark=false;
messages+="<li>请输入 <b>类别介绍!</b></li>";
}
request.setAttribute("messages",messages);
return mark;
}
/**
* @功能 后台-增加文章类别
*/
public void addArticleType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String messages = "";
String href="";
String forward="";
boolean flag=validateType(request,response);
if(flag){
ArticleTypeBean typeBean = new ArticleTypeBean();
typeBean.setTypeName(MyTools.toChinese(request.getParameter("typeName")));
typeBean.setTypeInfo(MyTools.toChinese(request.getParameter("typeInfo")));
ArticleTypeDao articleTypeDao = new ArticleTypeDao();
boolean mark=articleTypeDao.operationArticleType("add", typeBean);
if(mark) {
messages+="<li>添加文章类别成功!</li>";
href="<a href='admin/article/ArticleTypeAdd.jsp'>[继续添加文章类别]</a>";
forward="/admin/success.jsp";
}
else {
messages+="<li>添加文章类别失败!</li>";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
forward="/admin/error.jsp";
}
request.setAttribute("messages",messages);
request.setAttribute("href",href);
}
else{
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
forward="/admin/error.jsp";
request.setAttribute("href",href);
}
RequestDispatcher rd=request.getRequestDispatcher(forward);
rd.forward(request,response);
}
public void selectArticleType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArticleTypeDao typeDao=new ArticleTypeDao();
List typelist=typeDao.queryTypeList();
request.setAttribute("typelist",typelist);
RequestDispatcher rd=request.getRequestDispatcher("/admin/article/ArticleTypeList.jsp");
rd.forward(request,response);
}
/**
* @功能 后台-修改文章类别
*/
public void modifyArticleType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd=null;
ArticleTypeBean typeBean=null;
ArticleTypeDao typeDao=new ArticleTypeDao();
String type=request.getParameter("type");
if(type==null)
type="";
if(!type.equals("doModify")){
int typeId=MyTools.strToint(request.getParameter("typeId"));
typeBean=typeDao.queryTypeSingle(typeId);
request.setAttribute("typeSingle",typeBean);
rd=request.getRequestDispatcher("/admin/article/ArticleTypeModify.jsp");
rd.forward(request,response);
}
else{
String messages="";
String href="";
String forward="";
boolean flag=validateType(request,response);
if(flag){
typeBean = new ArticleTypeBean();
typeBean.setId(MyTools.strToint(request.getParameter("typeId")));
typeBean.setTypeName(MyTools.toChinese(request.getParameter("typeName")));
typeBean.setTypeInfo(MyTools.toChinese(request.getParameter("typeInfo")));
boolean mark=typeDao.operationArticleType("modify",typeBean);
if (mark) {
messages="<li>修改类别成功!</li>";
href="<a href='ArticleServlet?action=typeSelect'>[继续修改其他类别]</a>";
forward="/admin/success.jsp";
} else {
messages="<li>修改失败!</li>";
href="<a href='javascript:window.history.go(-1)>[返回]</a>";
forward="/admin/error.jsp";
}
request.setAttribute("messages",messages);
request.setAttribute("href",href);
}
else{
href="<a href='javascript:window.history.go(-1)>[返回]</a>";
forward="/admin/error.jsp";
request.setAttribute("href",href);
}
rd=request.getRequestDispatcher(forward);
rd.forward(request,response);
}
}
/**
* @功能 后台-删除文章类别
*/
public void deleteArticleType(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ArticleTypeDao typeDao = new ArticleTypeDao();
ArticleTypeBean typeBean = new ArticleTypeBean();
String messages="";
String href="";
String forward="";
typeBean.setId(MyTools.strToint(request.getParameter("typeId")));
boolean mark=typeDao.operationArticleType("delete",typeBean);
if (mark) {
messages+="<li>删除类别成功!</li>";
href="<a href='ArticleServlet?action=typeSelect'>[继续删除其他类别]</a>";
forward="/admin/success.jsp";
} else {
messages+="<li>删除类别失败!</li>";
href="<a href='javascript:window.history.go(-1)'>[返回]</a>";
forward="/admin/error.jsp";
}
request.setAttribute("messages",messages);
request.setAttribute("href",href);
RequestDispatcher rd = request.getRequestDispatcher(forward);
rd.forward(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -