📄 addnewsaction.java
字号:
/**
* 添加新的news的Action Bean
* AddNewsAction.java
* @author usr
*/
package building;
import java.sql.*;
import java.util.Vector;
import javax.sql.*;
import javax.servlet.http.*;
import javax.servlet.ServletContext;
import org.apache.struts.action.*;
public final class AddNewsAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)
throws Exception
{
DynaActionForm newsForm = (DynaActionForm)form;
String title = (String)newsForm.get("title");
String contents = (String)newsForm.get("contents");
//CharSet
title = CharSet.GBK_ISO(title);
contents = CharSet.GBK_ISO(contents);
//
HttpSession session = request.getSession();
Admin thisAdmin = (Admin)session.getAttribute(Constants.ADMIN_INFO_KEY);
//
News news = new News();
news.setId(0);
news.setAdderId(thisAdmin.getId());
news.setTitle(title);
news.setContents(contents);
news.setAddTime(ChinaTime.getTime());
//
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
DB db = new DB(dataSource);
//
String PageForward = null;
ActionMessages errors = new ActionMessages();
if ( thisAdmin.getLev() != 1 )
{
//此用户无权限
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.adminNoRight2ADD"));
if (!errors.isEmpty())
{
saveErrors(request,errors);
}//End of if
PageForward = "toWrong";
}//End of if
else
{
//此用户拥有权限
if ( news.addNews(db) )
{
//添加成功
PageForward = "toAddNewsOK";
}//End of if
else
{
//添加失败
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.addNewsFail"));
if (!errors.isEmpty())
{
saveErrors(request,errors);
}//End of if
PageForward = "toWrong";
}//End of else
}//End of else
//Close the Connection
db.close();
return(mapping.findForward(PageForward));
}//End of execute
}//End of class AddNewsAction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -