📄 albumaction.java
字号:
package dummies.struts.music;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
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.util.ModuleException;
import org.apache.struts.validator.DynaValidatorForm;
/**
* @author Mike Robinson
*
*/
public class AlbumAction extends Action
{
Log log = LogFactory.getLog(AlbumAction.class); // commons logging reference
/**
* Handles request from user
* @param mapping
* @param form
* @param request
* @param response
* @throws Exception
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// deterine the action. values can be null, 'save', 'update', or 'cancel'
String action = (String)((DynaValidatorForm)form).get("action1");
// action2 can be either null, empty or contain 'view' or 'delete'
String action2 = (String) request.getParameter("action2"); // comes from musiclist
if(action2!=null)
if(!action2.equals(""))
action = action2; // replace action with action2 if action2 is not empty
if((action == null)|(action.equals(""))) // request came from musiclist to create an Album
{
ServletContext sc = this.getServlet().getServletContext();
((DynaValidatorForm)form).set("years",(ArrayList)sc.getAttribute("years"));
((DynaValidatorForm)form).set("types",(ArrayList)sc.getAttribute("types"));
((DynaValidatorForm)form).set("categories",(ArrayList)sc.getAttribute("categories"));
return (mapping.findForward("new"));
}
else if(action.equalsIgnoreCase("view")) // request came from musiclist to edit an Album
{
// get the id of the album
int id = convertID((String) request.getParameter("id"));
if(id > 0)
{
// create a new AlbumBean passing the datasource
AlbumBean ab = new AlbumBean(getDataSource(request, "musiccollection"));
AlbumDTO album = ab.findAlbum(id);
ServletContext sc = this.getServlet().getServletContext();
xferToForm(album,form,sc);
}
return (mapping.findForward("new"));
}
else if(action.equalsIgnoreCase("delete")) // request came from musiclist to delete an Album
{
// get the id of the album
int id = convertID((String) request.getParameter("id"));
if(id > 0)
{
// create a new AlbumBean passing the datasource
AlbumBean ab = new AlbumBean(getDataSource(request, "musiccollection"));
ab.deleteAlbum(id);
}
}
else if (action.equalsIgnoreCase("cancel")) // request to abandon adding/modifying an album
{
return(mapping.findForward("cancel"));
}
else if ((action.equalsIgnoreCase("save"))|(action.equalsIgnoreCase("update")))
{
ActionErrors errors = ((DynaValidatorForm)form).validate(mapping,request);
if(errors.isEmpty())
{
// get the session object
HttpSession session = request.getSession();
// get the user object
UserDTO user = (UserDTO)session.getAttribute("user");
// initialize a fresh AlbumDTO
AlbumDTO album = new AlbumDTO();
// move form info to album
xferToBean(form,album,action,user);
// create a new AlbumBean passing the datasource
AlbumBean ab = new AlbumBean(getDataSource(request, "musiccollection"));
// if action == save, insert the new album
if(action.equalsIgnoreCase("save"))
ab.saveAlbum(album);
else // must need to update existing album
ab.updateAlbum(album);
}
else // there were validation errors
{
ServletContext sc = this.getServlet().getServletContext();
((DynaValidatorForm)form).set("years",(ArrayList)sc.getAttribute("years"));
((DynaValidatorForm)form).set("types",(ArrayList)sc.getAttribute("types"));
((DynaValidatorForm)form).set("categories",(ArrayList)sc.getAttribute("categories"));
saveErrors(request, errors);
return (mapping.findForward("failure"));
}
}
return (mapping.findForward("success"));
}
/**
* transfer bean data to form
* @param album
* @param form
*/
private void xferToForm(AlbumDTO album, ActionForm form, ServletContext sc)
{
((DynaValidatorForm)form).set("album",album.getAlbum());
((DynaValidatorForm)form).set("artist",album.getArtist());
((DynaValidatorForm)form).set("description",album.getDescription());
((DynaValidatorForm)form).set("year",album.getYear());
((DynaValidatorForm)form).set("category",album.getCategory());
((DynaValidatorForm)form).set("type",album.getType());
((DynaValidatorForm)form).set("id",String.valueOf(album.getId()));
((DynaValidatorForm)form).set("userid",String.valueOf(album.getUserid()));
((DynaValidatorForm)form).set("years",(ArrayList)sc.getAttribute("years"));
((DynaValidatorForm)form).set("types",(ArrayList)sc.getAttribute("types"));
((DynaValidatorForm)form).set("categories",(ArrayList)sc.getAttribute("categories"));
}
/**
* transfer data from form to bean
* @param form
* @param album
* @param action
* @param user
* @throws ModuleException
*/
private void xferToBean(ActionForm form, AlbumDTO album, String action, UserDTO user) throws ModuleException
{
album.setAlbum((String)((DynaValidatorForm)form).get("album"));
album.setArtist((String)((DynaValidatorForm)form).get("artist"));
album.setDescription((String)((DynaValidatorForm)form).get("description"));
album.setYear((String)((DynaValidatorForm)form).get("year"));
album.setType((String)((DynaValidatorForm)form).get("type"));
album.setCategory((String)((DynaValidatorForm)form).get("category"));
if(action.equalsIgnoreCase("update"))
{
try
{
album.setUserid(Integer.parseInt((String)((DynaValidatorForm)form).get("userid")));
album.setId(Integer.parseInt((String)((DynaValidatorForm)form).get("id")));
}
catch(NumberFormatException nfe)
{
log.error("error in converting string to a number");
log.error(nfe.getLocalizedMessage());
ModuleException me = new ModuleException("error.nfe.message");
throw me;
}
}
else // assume creating a new album, so no userID or ID available in form
{
album.setUserid(user.getId());
album.setId(0);
}
}
/**
* convert String id to numeric
* @param id
* @return int
* @throws ModuleException
*/
private int convertID(String id) throws ModuleException
{
int idNum = 0;
if(id != null) // id should contain the id of the album to delete
if(!id.equals("")) // then user is request to delete an album
{
// convert the String id to int id
try
{
idNum = Integer.parseInt(id);
}
catch(NumberFormatException nfe)
{
log.error("error in converting string to a number");
log.error(nfe.getLocalizedMessage());
ModuleException me = new ModuleException("error.nfe.message");
throw me;
}
}
return idNum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -