Struts实现登陆
2009-08-05 15:30 薛凯凯圆滚滚 阅读(624) 评论(0) 编辑 收藏 举报![](https://www.cnblogs.com/Emoticons/baimantou/202015214.gif)
不知道1.1版本里是否有使用到这个类。就先用Action这个类写一遍。
比如:现在你已经在login.jsp页面提交给Struts,或者index.jsp页面提交任何都行。
配置文件:
web.xml
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
struts-config.xml配置<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
package web.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class loginForm extends ActionForm {
private String username;
private String password;
public loginForm() {
// TODO Auto-generated constructor stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
ActionErrors error = new ActionErrors();
if(username==null||username.equals("")||username.length()<1)
{
error.add("username",new ActionMessage("error.username.reqired"));
}
if(password==null||"".equals(password)||password.length()<1)
{
error.add("password",new ActionMessage("error.password.reqired"));
}
return error;
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class loginForm extends ActionForm {
private String username;
private String password;
public loginForm() {
// TODO Auto-generated constructor stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
ActionErrors error = new ActionErrors();
if(username==null||username.equals("")||username.length()<1)
{
error.add("username",new ActionMessage("error.username.reqired"));
}
if(password==null||"".equals(password)||password.length()<1)
{
error.add("password",new ActionMessage("error.password.reqired"));
}
return error;
}
}
LoginAction类
ackage web.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import app.Constants;
import com.UserDirectory;
import com.UserDirectoryException;
import web.form.loginForm;
public class loginAction extends DispatchAction {
public loginAction() {
// TODO Auto-generated constructor stub
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
loginForm myform = (loginForm)form;
if(myform==null)
{
return new ActionForward(mapping.getInput(),false);
}
boolean istrue = false;
try
{
//验证登陆业务逻辑
istrue = isUserlogon(myform.getUsername(),myform.getPassword());
}
catch (Exception e) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("error.logon.connect"));
saveErrors(request, errors);
return new ActionForward(mapping.getInput(),false);
}
if(!istrue)
{
ActionErrors errors = new ActionErrors();
//1.1以后没有ActionError类 使用ActionMessage
//1.0 errors.add(ActionError.GLOBAL_MESSAGE,new ActionError("
."))
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.logon.invalid"));
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
HttpSession session = request.getSession(true);
session.setAttribute(Constants.USER_KEY,myform);
/*
1.0时
if(servlet.getDebug()>=Constants.DEBUG)
*/
if(Integer.parseInt(servlet.getInitParameter("debug"))>=Constants.DEBUG)
{
StringBuffer msg = new StringBuffer("LogonUser :'");
msg.append(myform.getUsername());
msg.append("'logged on in session");
msg.append(session.getId());
servlet.log(msg.toString());
}
return mapping.findForward(Constants.SUCESS);
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import app.Constants;
import com.UserDirectory;
import com.UserDirectoryException;
import web.form.loginForm;
public class loginAction extends DispatchAction {
public loginAction() {
// TODO Auto-generated constructor stub
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
loginForm myform = (loginForm)form;
if(myform==null)
{
return new ActionForward(mapping.getInput(),false);
}
boolean istrue = false;
try
{
//验证登陆业务逻辑
istrue = isUserlogon(myform.getUsername(),myform.getPassword());
}
catch (Exception e) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("error.logon.connect"));
saveErrors(request, errors);
return new ActionForward(mapping.getInput(),false);
}
if(!istrue)
{
ActionErrors errors = new ActionErrors();
//1.1以后没有ActionError类 使用ActionMessage
//1.0 errors.add(ActionError.GLOBAL_MESSAGE,new ActionError("
![](https://www.cnblogs.com/Images/dot.gif)
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.logon.invalid"));
saveErrors(request, errors);
return new ActionForward(mapping.getInput());
}
HttpSession session = request.getSession(true);
session.setAttribute(Constants.USER_KEY,myform);
/*
1.0时
if(servlet.getDebug()>=Constants.DEBUG)
*/
if(Integer.parseInt(servlet.getInitParameter("debug"))>=Constants.DEBUG)
{
StringBuffer msg = new StringBuffer("LogonUser :'");
msg.append(myform.getUsername());
msg.append("'logged on in session");
msg.append(session.getId());
servlet.log(msg.toString());
}
return mapping.findForward(Constants.SUCESS);
}
}