目錄結構:
SimpleExamples
|_ index.jsp
|_ pages
| |_ Welcome.jsp
| |_ logon.jsp
|_ pic
| |_
|_ WEB-INF
|_ classes
| |_ ApplicationResources.properties
| |_ strutsinactionexamples
| |_ LogonAction.class
| |_ LogonForm.class
| |_ LogoffAction.class
|_ lib
| |_ jdbc2_0-stdext.jar
| |_ struts.jar
|_ struts-bean.tld
|_ struts-html.tld
|_ struts-logic.tld
|_ struts-config.xml
|_ web.xmlsss
/*---------------------------ApplicationResources.properties-----------------------------------*/
# -- standard errors --
errors.header=
Validation Error
You must correct the following error(s) before proceeding:
error.username.required=
Username is requirederror.password.required=
Password is requirederror.logon.connect=
Connectiong is failureerror.logon.invalid=
Username/password combination is invalid
# -- welcome --
welcome.title=Welcome !
/*-------------------Constants.jsp ---------------------*/
package strutsinactionexamples;
public final class Constants {
/**
* 當前用戶的用戶名在session中的id
*/
public static final String USER_KEY = "user";
/**
* 除錯信息的值
*/
public static final int DEBUG = 1;
/**
* 正常訊息的值
*/
public static final int NORMAL = 0;
/**
* ActionForward中的正常結果
*/
public static final String SUCCESS = "success";
/**
* 代表ActionForward 中地正常結果的符號
*/
public static final String LOGON = "logon";
/**
* 這個符號代表ActionForward中的登陸行動的符號
*/
public static String WELCOME = "welcome";
}
/*-------------------------LogoffAction.java--------------------------*/
package strutsinactionexamples;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
public class LogoffAction extends Action {
public ActionForward perform(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
javax.servlet.http.HttpSession session=servletRequest.getSession();
LogonForm user=(LogonForm)session.getAttribute(Constants.USER_KEY);
if(user!=null){
if(servlet.getDebug()>=Constants.NORMAL){
StringBuffer message=new StringBuffer("LogoffAcion:user '");
message.append(user.getUsername());
message.append("' logged off in session ");
message.append(session.getId());
servlet.log(message.toString());
}
}
session.removeAttribute(Constants.USER_KEY);
//session.invalidate();//同時也會摧毀本地話物件
return (actionMapping.findForward(Constants.SUCCESS));
}
}
/*--------------------LogonAction .java----------------*/
package strutsinactionexamples;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.servlet.http.HttpSession;
public class LogonAction extends Action {
public LogonAction() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ActionForward perform(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
LogonForm logonForm = (LogonForm) actionForm;
String username = logonForm.getUsername();
String password = logonForm.getPassword();
boolean validated = false;
try {
validated = this.isUserLogon(username, password);
} catch (Exception e) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.logon.connect"));
saveErrors(servletRequest, errors);
return (new ActionForward(actionMapping.getInput())); //struts-config裡面一定要些input='xx'
}
if (!validated) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.logon.invalid"));
saveErrors(servletRequest, errors);
return (new ActionForward(actionMapping.getInput())); //回到輸入頁
} else {
HttpSession session = servletRequest.getSession();
session.setAttribute(Constants.USER_KEY, logonForm);
//form存入session中表示簽入成功
if (servlet.getDebug() >= Constants.NORMAL) { //出錯層級大於一定時記錄,ActionServlet.getDebug 取得<init-param>
debug>2>>中得數值
StringBuffer message = new StringBuffer("LogonAciont:User '");
message.append(username);
message.append("' logged on is session ");
message.append(session.getId());
servlet.log(message.toString());
}
return (actionMapping.findForward(Constants.SUCCESS));
}
}
private boolean isUserLogon(String username, String password) {
return (username.equals("a") && password.equals("a"));
}
private void jbInit() throws Exception {
}
}
/*---------------------LogonForm.java---------------------*/
package strutsinactionexamples;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class LogonForm extends ActionForm {
private String password;
private String username;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
ActionErrors errors=new ActionErrors();
if((this.username==null)||(this.username.length()<1))
errors.add("username",new ActionError("error.username.required"));
if((this.password==null)||(this.password.length()<1))
errors.add("password",new ActionError("error.password.required"));
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
password = null;
username = null;
}
}
/*------------------web.xml------------------------------*/
http://java.sun.com/dtd/web-app_2_3.dtd">
WebModuleStruts1_0
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
2
application
ApplicationResources
2
action
*.do
index.jsp
index.html
index.htm
/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld
/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld
/WEB-INF/struts-logic.tld
/WEB-INF/struts-logic.tld
/WEB-INF/struts-template.tld
/WEB-INF/struts-template.tld
/*------------------------struts-config.xml------------------------------*/
http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
/*----------------------index.jsp-----------------------*/
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%--/Welcome.do不可以,