Pessoal,
Estou iniciando meu estudo em struts 1x, e no primeiro exemplo do livro não está rodando, segue os meus arquivos, estrutura e no final a msg de erro.
Obrigado,
RegisterForm.java
package app;
import org.apache.struts.action.ActionForm;
public class RegisterForm extends ActionForm {
protected String username;
protected String password1;
protected String password2;
public String getUsername() {
return this.username;
}
public String getPassword1() {
return this.password1;
}
public String getPassword2() {
return this.password2;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword1(String password) {
this.password1 = password;
}
public void setPassword2(String password) {
this.password2 = password;
}
}
RegisterAction.java
package app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class RegisterAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) {
RegisterForm rf = (RegisterForm) form;
String username = rf.getUsername();
String password1 = rf.getPassword1();
String password2 = rf.getPassword2();
if (password1.equals(password2)) {
try {
UserDirectory.getInstance().setUser(username, password1);
} catch (UserDirectoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("sucess");
}
return mapping.findForward("failure");
}
}
UserDirectoryException.java
[code]/*
- $Header: /home/cvsroot/SIA_Chap1/WEB-INF/src/app/UserDirectoryException.java,v 1.1 2004/06/18 18:20:25 mengho Exp $
- $Revision: 1.1 $
- $Date: 2004/06/18 18:20:25 $
- ====================================================================
- The Apache Software License, Version 1.1
- Copyright © 1999 The Apache Software Foundation. All rights
- reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
- The end-user documentation included with the redistribution, if
- any, must include the following acknowlegement:
-
"This product includes software developed by the
-
Apache Software Foundation (http://www.apache.org/)."
- Alternately, this acknowlegement may appear in the software itself,
- if and wherever such third-party acknowlegements normally appear.
-
- The names “The Jakarta Project”, “Tomcat”, and "Apache Software
- Foundation" must not be used to endorse or promote products derived
- from this software without prior written permission. For written
- permission, please contact apache@apache.org.
-
- Products derived from this software may not be called “Apache”
- nor may “Apache” appear in their names without prior written
- permission of the Apache Group.
- THIS SOFTWARE IS PROVIDED ``AS IS’’ AND ANY EXPRESSED OR IMPLIED
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
- ====================================================================
- This software consists of voluntary contributions made by many
- individuals on behalf of the Apache Software Foundation. For more
- information on the Apache Software Foundation, please see
- <http://www.apache.org/>.
*/
/**
- @author George Franciscus
-
@version $Revision: 1.1 $ $Date: 2004/06/18 18:20:25 $
*/
package app;
public class UserDirectoryException extends Exception {
// ; Empty implementation
}[/code]
UserDirectory
[code]/*
- $Header: /home/cvspublic/jakarta-struts/src/example/org/apache/struts/example/LogoffAction.java,v 1.4 2000/09/23 22:53:53 craigmcc Exp $
- $Revision: 1.4 $
- $Date: 2000/09/23 22:53:53 $
- ====================================================================
- The Apache Software License, Version 1.1
- Copyright © 1999 The Apache Software Foundation. All rights
- reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
- The end-user documentation included with the redistribution, if
- any, must include the following acknowlegement:
-
"This product includes software developed by the
-
Apache Software Foundation (http://www.apache.org/)."
- Alternately, this acknowlegement may appear in the software itself,
- if and wherever such third-party acknowlegements normally appear.
-
- The names “The Jakarta Project”, “Tomcat”, and "Apache Software
- Foundation" must not be used to endorse or promote products derived
- from this software without prior written permission. For written
- permission, please contact apache@apache.org.
-
- Products derived from this software may not be called “Apache”
- nor may “Apache” appear in their names without prior written
- permission of the Apache Group.
- THIS SOFTWARE IS PROVIDED ``AS IS’’ AND ANY EXPRESSED OR IMPLIED
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
- ====================================================================
- This software consists of voluntary contributions made by many
- individuals on behalf of the Apache Software Foundation. For more
- information on the Apache Software Foundation, please see
- <http://www.apache.org/>.
*/
/**
- Implementation of <strong>Action</strong> that validates a user
- logon.
-
- :TODO: Javadocs.
- @todo Javadocs
- @author George Franciscus
-
@version $Revision: $ $Date: $
*/
package app;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class UserDirectory {
/**
*
*/
private static final String UserDirectoryFile = "resources/users.properties";
/**
*
*/
private static final String UserDirectoryHeader = "${user}=${password}";
/**
*
*/
private static UserDirectory userDirectory = null;
/**
*
*/
private static Properties p;
/**
*
*/
private UserDirectory() throws UserDirectoryException {
java.io.InputStream i = null;
p = null;
i = this.getClass().getClassLoader().getResourceAsStream(
UserDirectoryFile);
if (null == i) {
throw new UserDirectoryException();
}
else {
try {
p = new Properties();
p.load(i);
i.close();
}
catch (java.io.IOException e) {
p = null;
System.out.println(e.getMessage());
throw new UserDirectoryException();
}
finally {
i = null;
}
} // end else
} // end UserDirectory
/**
*
*/
public static UserDirectory getInstance() throws UserDirectoryException {
if (null == userDirectory) {
userDirectory = new UserDirectory();
}
return userDirectory;
}
/**
* Transform id so that it will match any conventions used by user
* directory. The default implementation forces the id to uppercase. Does
* <b>not</b> expect the userId to be null and will throw a NPE if it is.
*
* @exception Throws
* Null Pointer Exception if userId is null.
*/
public String fixId(String userId) {
return userId.toUpperCase();
}
/**
*
*/
public boolean isValidPassword(String userId, String password) {
// no null passwords
if (null == password)
return false;
// conform userId to uppercase
String _userId = fixId(userId);
// no passwords for non-users
if (!isUserExist(_userId))
return false;
// does password match user's password
return (password.equals(getPassword(_userId)));
}
/**
*
*/
public boolean isUserExist(String userId) {
// no null users
if (null == userId)
return false;
// if not null, it's a user
return !(null == p.getProperty(userId));
}
/**
*
*/
public String getPassword(String userId) {
return p.getProperty(userId);
}
/**
*
*/
public Enumeration getUserIds() {
return p.propertyNames();
}
/**
*
*/
public void setUser(String userId, String password)
throws UserDirectoryException {
// no nulls
if ((null == userId) || (null == password)) {
throw new UserDirectoryException();
}
try {
// conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(
UserDirectoryFile).getFile();
p.store(new FileOutputStream(o), UserDirectoryHeader);
}
catch (IOException e) {
throw new UserDirectoryException();
}
}
} // end UserDirectory
[/code]
register.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="form" %>
<form:form action="register.do">
Username:<form:text property="username"/>
Enter password:<form:password property="password1"/>
re-enter password:<form:password property="password2"/>
<form:submit value="Register"/>
</form:form>
struts-config.xml
[code]<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.2//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd”>
<struts-config>
<form-beans>
<form-bean
name="registerForm"
type="app.RegisterForm"/>
</form-beans>
<action-mappings>
<action
path="/register"
type="app.RegisterAction"
name="registerForm">
<forward name="success" path="/success.html"/>
<forward name="failure" path="/failure.html"/>
</action>
</action-mappings>
</struts-config>[/code]
web.xml
[code]<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN”
“http://java.sun.com/j2ee/dtds/web-app_2_2.dtd”>
<web-app>
<!-- Action Servlet Configuration -->
<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>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>[/code]
[img]
[/img]
Mensagem de erro
16/09/2008 20:20:17 org.apache.catalina.loader.WebappClassLoader modified
INFO: Additional JARs have been added : 'standard-1.0.6.jar'
16/09/2008 20:20:17 org.apache.catalina.core.StandardContext reload
INFO: Reloading this Context has started
16/09/2008 20:20:23 org.apache.struts.action.ActionServlet initChain
INFO: Loading chain catalog from jar:file:/C:/java/Tomcat%205.5/webapps/register/WEB-INF/lib/struts-core-1.3.8.jar!/org/apache/struts/chain/chain-config.xml
16/09/2008 20:20:24 org.apache.struts.util.PropertyMessageResources loadLocale
WARNING: Resource org/apache/struts/action/ActionResources_pt_BR.properties Not Found.
16/09/2008 20:20:24 org.apache.struts.util.PropertyMessageResources loadLocale
WARNING: Resource org/apache/struts/action/ActionResources_pt.properties Not Found.
16/09/2008 20:20:24 org.apache.struts.action.ActionServlet splitAndResolvePaths
SEVERE: Missing configuration resource for path /WEB-INF/struts-config.xml
16/09/2008 20:20:24 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet action as unavailable
16/09/2008 20:20:24 org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /register threw load() exception
javax.servlet.UnavailableException: Missing configuration resource for path /WEB-INF/struts-config.xml
at org.apache.struts.action.ActionServlet.splitAndResolvePaths(ActionServlet.java:1872)
at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:683)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:356)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3956)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4230)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3025)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:432)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1278)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1570)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1579)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1579)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1559)
at java.lang.Thread.run(Unknown Source)