//------------------LogonForm-----------------package addressbook.forms;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* <strong>LogonForm</strong> handles the form
* that the user will use to logon to the application.
*/public final class LogonForm extends ActionForm {
private String username = null;
private String password = null;
public String getUserName() {
return (this.username);
} public void setUserName(String username) {
this.username = username;
}
public String getPassword() {
return (this.password);
} public void setPassword(String password) {
this.password = password;
} public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
this.password = null; } public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if ((username == null) || (username.length() < 1))
//errors.add("username", new ActionMessage("error.username.required"));
{
errors.add("username", new ActionMessage("com.jeffaple.struts"));
System.out.println("username can not be null!!");
}
if ((password == null) || (password.length() < 1))
errors.add("password", new ActionMessage("error.password.required")); return errors; }
}//---------------logon.jsp-----------------
<%@ include file="taglibs.jsp" %>
<%@ include file="header.jsp" %>
<%@ include file="logonContent.jsp" %>
<%@ include file="footer.jsp" %>//---------------logonContent.jsp------------
<%@ include file="taglibs.jsp" %><html:errors/>
<html:form action="/logon.do" focus="userName" >
<center>
<table border="0" width="100%"> <tr>
<th align="right">
<bean:message key="prompt.username"/>
</th>
<td align="left">
<html:text property="userName" size="15" maxlength="15"/>
</td>
</tr> <tr>
<th align="right">
<bean:message key="prompt.password"/>
</th>
<td align="left">
<html:password property="password" size="15" maxlength="15"
redisplay="false"/>
</td>
</tr> <tr>
<td align="right">
<html:submit property="submit" >
<bean:message key="button.logon"/>
</html:submit> </td>
<td align="left">
<html:reset >
<bean:message key="button.reset"/>
</html:reset> </td>
</tr></table>
</center>
</html:form>
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* <strong>LogonForm</strong> handles the form
* that the user will use to logon to the application.
*/public final class LogonForm extends ActionForm {
private String username = null;
private String password = null;
public String getUserName() {
return (this.username);
} public void setUserName(String username) {
this.username = username;
}
public String getPassword() {
return (this.password);
} public void setPassword(String password) {
this.password = password;
} public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
this.password = null; } public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if ((username == null) || (username.length() < 1))
//errors.add("username", new ActionMessage("error.username.required"));
{
errors.add("username", new ActionMessage("com.jeffaple.struts"));
System.out.println("username can not be null!!");
}
if ((password == null) || (password.length() < 1))
errors.add("password", new ActionMessage("error.password.required")); return errors; }
}//---------------logon.jsp-----------------
<%@ include file="taglibs.jsp" %>
<%@ include file="header.jsp" %>
<%@ include file="logonContent.jsp" %>
<%@ include file="footer.jsp" %>//---------------logonContent.jsp------------
<%@ include file="taglibs.jsp" %><html:errors/>
<html:form action="/logon.do" focus="userName" >
<center>
<table border="0" width="100%"> <tr>
<th align="right">
<bean:message key="prompt.username"/>
</th>
<td align="left">
<html:text property="userName" size="15" maxlength="15"/>
</td>
</tr> <tr>
<th align="right">
<bean:message key="prompt.password"/>
</th>
<td align="left">
<html:password property="password" size="15" maxlength="15"
redisplay="false"/>
</td>
</tr> <tr>
<td align="right">
<html:submit property="submit" >
<bean:message key="button.logon"/>
</html:submit> </td>
<td align="left">
<html:reset >
<bean:message key="button.reset"/>
</html:reset> </td>
</tr></table>
</center>
</html:form>
解决方案 »
- Could not resolve placeholder 'hibernate.driverClassName'
- JSP问题 急!
- 我对计算机一窍不通,如何才能进入计算机行业工作呢?
- 《Spring 从入门到精通》
- 问一个<bean:write>的问题!!
- jboss中客户请求中断后对javabean对数据库连接池的释放问题。分不高,在于讨论技术。
- 在使用taglib编程时,出现:Unable to find setter method for attribute
- 请问为什么tomcat不能装在80端口呢?
- 我想问一下P4下安装oracle问题,分多啊快来啊!
- JB里面怎么设置getter和setter方法?
- 问一个ibatis中的一个问题
- 求教Hibernate中xml配置文件字段数据类型错误
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ============ Data Source =================================== --> <!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="logonForm"
type="addressbook.forms.LogonForm"/>
<form-bean name="searchForm"
type="addressbook.forms.SearchForm"/>
<form-bean name="insertForm"
type="addressbook.forms.InsertForm"/>
</form-beans>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward name="logoff" path="/logoff.do"/>
<forward name="logon" path="/logon.jsp"/>
<forward name="success" path="/mainMenu.jsp"/>
<forward name="search" path="/search.jsp"/>
<forward name="displayall" path="/displayall.do"/>
<forward name="insert" path="/insert.jsp"/>
<forward name="mainMenu" path="/mainMenu.jsp"/>
<forward name="confirmation" path="/confirmation.jsp"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action path="/search"
type="addressbook.actions.SearchAction"
name="searchForm"
attribute="myForm"
scope="request"
input="/search.jsp">
<forward name="success" path="/display.jsp"/>
</action>
<action path="/displayall"
type="addressbook.actions.DisplayAllAction"
name="nestedForm"
scope="request"
input="/mainMenu.jsp">
<forward name="success" path="/display.jsp"/>
</action>
<action path="/insert"
type="addressbook.actions.InsertAction"
name="insertForm"
scope="request"
input="/insert.jsp"
validate="true">
</action>
<!-- Process a user logoff -->
<action path="/logoff"
type="addressbook.actions.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action> <!-- Process a user logon -->
<action path="/logon"
type="addressbook.actions.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp">
</action>
</action-mappings>
<message-resources parameter="addressbook.ApplicationResources"/></struts-config>//-----------------LogonAction.java--------------------------
package addressbook.actions;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import addressbook.Constants;
import addressbook.model.UserBean;
import addressbook.forms.LogonForm;/**
* <strong>LogonAction</strong> provides the login mechanism
* for the sample application. The username/password pair that
* is entered by the user will be compared against the database
* values that where read in at application startup. An error will
* be reported if the username/password don't match, otherwise
* a <code>User</code> Object will be inserted into the session
* to indicate that the user is currently logged in.
*/
public final class LogonAction extends AbstActionBase {
private Log log =
LogFactory.getLog(this.getClass().getName());
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { String userName=null;
String password=null;
Locale locale = getLocale(request);
MessageResources messages = getResources(request); UserBean user = null;
// Demonstrate a trace call to the logger
if (log.isTraceEnabled()) {
log.trace("LogonAction: entering method");
}
// Validate the request parameters specified by the user
ActionMessages errors = new ActionMessages();
if (form != null){
userName = ((LogonForm) form).getUserName();
password = ((LogonForm) form).getPassword();
}
Hashtable database = (Hashtable)
servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
if (database == null)
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.database.missing"));
else {
user = (UserBean) database.get(userName);
if ((user != null) && !user.getPassword().equals(password)){
user = null;
}
if (user == null)
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.password.mismatch"));
} // Report any errors we have discovered back to the original form
if (!errors.isEmpty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// Save our logged-in user in the session
HttpSession session = request.getSession();
session.setAttribute(Constants.USER_KEY, user);
// Demonstrate a debug call to the logger
if (log.isDebugEnabled()) {
log.debug("LogonAction: User '" + user.getUserName() +
"' logged on in session " + session.getId());
}
// Remove the obsolete form bean
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
} // Forward control to the specified success URI
return (mapping.findForward(Constants.FORWARD_SUCCESS)); }}
显示所有的错误信息.
如果要一条一条显示,用
<html:errors property=""/>
显示所有的错误信息.?那是什么时候把error.header和error.footer加进去的呢?
在form里面 validate时候,只有 errors.add("username", new ActionMessage("error.username.required"));?
是自动加进去的
每一个显示error的地方都会包含这个。主要用来做error的特殊显示。