Action:单实例(单例),多线程使用时如果有可以修改的成员属性要注意线程安全
ActionForm一般是粗粒度的,一般一个模块对应一个ActionForm
Action:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
// if ("admin".equals(username) && "admin".equals(password)) {
// //转向到登录成功页面
// request.setAttribute("username", username);
// return mapping.findForward("success");
// }else {
// //转向到登录失败页面
// return mapping.findForward("error");
// }
String errorInfo = "";
try {
UserManager.getInstance().login(username, password);
//request.setAttribute("username", username);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
unfe.printStackTrace();
errorInfo = "用户不能找到,用户名称=[" + username + "]";
}catch(PasswordErrorException pee) {
pee.printStackTrace();
errorInfo = "密码错误";
}
request.setAttribute("errorinfo", errorInfo);
return mapping.findForward("error");
}
ActionForm 两个属性:username password
注意ActionForm里面的两个方法:
方法/**
* 每次收集数据都调用
*/
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------LoginActionForm.reset()-----------");
}
/**
* 每次收集数据都调用
*/
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("----------LoginActionForm.validate()-----------");
return null;
}
配置文件:
<form-beans>
<form-bean name="loginForm" type="com.aowin.struts.LoginActionForm"/>
</form-beans>
<!-- path:访问路径 ,必须以/开头 服务器端很多都必须以/开头,Servlet也是
name:用哪个ActionForm
scope:默认sessino 不要放到session 因为这个对象用完就没了.
默认是session
一个Action对应一个ActionMapping实例
validate:默认true
-->
<action-mappings>
<action path="/login"
type="com.aowin.struts.LoginAction"
name="loginForm"
scope="request"
validate="true"
>
<forward name="success" path="/login_success.jsp" />
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
页面可以取ActionForm的value
<body>
<%
LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm");
%>
<%=laf.getUsername() %>,登录成功
</body>
假如的struts1的jar:参照发行包里的空项目