📄 loginaction.java
字号:
package com.oreilly.actions;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.oreilly.forms.LoginForm;
/**
* <strong>LoginAction</strong> demonstrates how an action class is called within the struts framework
* For the purposes of this sample, we simple demonstrate how the perform is called,
* a sample action, and a return
*
*@author Sue Spielman Switchback Software LLC www.switchbacksoftware.com
*/
public class LoginAction extends AbstStrutsActionBase
{
/**
* Black box method used to authenticate a user. There would typically
* be a bean (or EJB) here that actually does the work.
* A lookup would be done (as shown in the base class) and create on the home interface
* to authenticate the user against some backend database.
*@param String containing username
*@param String containing password
*@return boolean - true authenticated, false not authenticated.
*/
public boolean authenticate(String username, String password)
{
// Do the appropriate lookup and calls.
// That code is not provided as part of this sample.
// Instead, it is just to demonstrate the interaction
// of the action class with business logic tier.
return(true);
}
/**
* Override base class with specific <strong>perform</strong> implementation
* for this class.
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional AbstActionFormBase bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
// Assume that the login fails, so that the login page is redisplayed
// if the user isn't authenticated
boolean validLogin=false;
ActionForward actionForward = mapping.findForward(LOGIN);
// Create the container for any errors that occur
ActionErrors errors = new ActionErrors();
// Extract attributes and parameters we will need from the incoming
// form.
LoginForm loginForm = (LoginForm) form;
String userName = null;
String password = null;
if (loginForm != null){
userName = loginForm.getUserName();
password = loginForm.getPassword();
validLogin = authenticate(userName, password);
}
if (validLogin){
// Forward control to the specified 'success' URI specified in the structs-config.xml
actionForward = mapping.findForward(SUCCESS);
// Save something into the session for later use.
request.getSession(true).setAttribute("USERNAME", userName);
} else {
errors.add("login", new ActionError("error.login.authenticate"));
}
// If any messages is required, save the specified error messages keys
// into the HTTP request for use by the <struts:errors> tag.
if (!errors.empty()) {
saveErrors(request, errors);
}
// Forward control to the appropriate URI as determined by the action.
return (actionForward);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -