Having spent a whole day to begin the study of Struts, I finally successfully to work out the first struts example in Ch2 of the book 《Struts与Hibernate使用教程》。I am so happy that I decide to record it in my blog.
My Develop Enviroment:Lomboz3.2+Eclipse3.2+Struts1.2.9。
1.new project:Struts, NOTICE to select "Struts project" in Configuration.-> Click Next -> Click Next -> choose Where sturct located in your hard disk.(D:/path/struts-1.2.9-bin/) -> finished.
Using this lomboz wizard to develop struts projects, lomboz copy files of struts-blank.war in folder path/struts-1.2.9-bin/webapps/ into the project for you such as all the needed .jar files in /lib. But it does not copy the .tld files into you project, so you have to copy it by hand.
2.In folder WEB-INF, new folder:tlds, and then copy the five .tld files of struts tags into tlds. The file files can be directly used in .jsp files like this: <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>. You need not declare them in web.xml. (It seems have to declare them in web.xml in previous version of web.xml) Of course, you have the rights not to copy them in the folder tlds in WEB-INF, then you have to use: <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> in .jsp files instead of: <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>.
3.In folder WebContent, new folder: login.
4.In folder login, new file: login.jsp, main.jsp, register,jsp.
their source is listed below:
------------------------------------------------
login.jsp
-------------------
<% ... @ page contentType="text/html; charset=UTF-8" %>
< html:html >
< head >
< title > Login page </ title >
</ head >
< body >
< html:form action ="LoginAction" method ="post" >
Username: < html:text property ="username" size ="15" /> < br />< br />
Password: < html:password property ="password" size ="15" /> < br />< br />
< html:submit property ="submit" value ="Login" />
</ html:form >
</ body >
</ html:html >
------------------------------------------------
main.jsp
-------------------
pageEncoding = " ISO-8859-1 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
< title > Success to login </ title >
</ head >
< body >
<% = session.getAttribute( " username " ) %> , You have login.
</ body >
</ html >
------------------------------------------------
register.jsp
-------------------
pageEncoding="ISO-8859-1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
< title > Failed login </ title >
</ head >
< body >
<% = session.getAttribute( " username " ) %> , you failed to login.
</ body >
</ html >
------------------------------------------------
5.in folder /src, create LoginHanlder.java that is used to handler whether the user and password are legal. In order to simple this example, I decide not to use database and assume the username is "myname" and the related password is "mypassword".
Source file is listed below:
------------------------------------------------
LoginHandler.java
-----------------------
import java.util. * ;
public class LoginHandler ... {
public LoginHandler()
...{
}
public boolean checkLogin(ArrayList al)...{
String name = (String)al.get(0);
String password = (String)al.get(1);
if(name.equals("myname") && password.equals("mypassword"))...{
return true;
} else ...{
return false;
}
}
}
------------------------------------------------
6. in folder src, create the ActionForm: LoginActionForm.java.
New ->choose "other" -> choose "Lomboz-Struts.Struts Form Bean, Next -> Choose "Add New Action Form", Next -> Java Package="login"; Class name="LoginActionForm.java", Next -> Add two properties: username and password, Finish.
------------------------------------------------
LoginActionForm.java.
------------------------
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginActionForm extends ActionForm ... {
private java.lang.String username;
private java.lang.String password;
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) ...{
ActionErrors errors = new ActionErrors();
// Validate an attribute named "xxx"
//if( getXXX() == null || getXXX().length() == 0 )
// errors.add("xxx",new ActionMessage("errors.required","xxx"));
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) ...{
super.reset(mapping, request);
}
public java.lang.String getUsername()...{
return username;
}
public void setUsername(java.lang.String username) ...{
this.username = username;
}
public java.lang.String getPassword()...{
return password;
}
public void setPassword(java.lang.String password) ...{
this.password = password;
}
}
------------------------------------------------
7. in folder src, create an Action: LoginAction.java.
New ->choose "other" -> choose "Lomboz-Struts.Struts Action, Next -> Choose "Create a new action", Next -> Java Package="login"; Class name="LoginAction.java", Next -> Action Form: select "LoginActionForm"; scope: select "request"; Validate: false; Input: choose "/login/login.jsp", Finish.
Then change the exxcute() method.
Source file is listed below:
------------------------------------------------
----------------
package login;
import java.io.IOException;
import java.util.ArrayList;
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.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action ... {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException ...{
LoginActionForm loginForm = (LoginActionForm)form;
String username = loginForm.getUsername();
String password = loginForm.getPassword();
HttpSession session = request.getSession(true);
session.removeAttribute("username");
session.setAttribute("username", username);
ArrayList al = new ArrayList();
al.add(username);
al.add(password);
String prompt;
LoginHandler login = new LoginHandler();
if(login.checkLogin(al))...{
prompt = "Success";
} else ...{
prompt = "Fail";
}
return mapping.findForward(prompt);
}
}
------------------------------------------------
8. change the sturts-config.xml.
when the username and password are legal, we should lead the guest to main.jsp, and when thery are illegal, we ask them to register.
add
< forward name ="Fail" path ="/login/register.jsp" />
below
OK, all are done. let's have a look at the result.
right click login.jsp and choose to Run on server.
The browser leads us to http://localhost:8080/Struts/login/login.do and tells us that:
------------------
type Status report
message Invalid path was requested
description The requested resource (Invalid path was requested) is not available.
--------------------
I was fooled by this for hours and could not sovle the problem. I do not know why it go to login.do. In a time, I changed the login.do to login.jsp and happy to find that it worked. so the whole address should be:http://localhost:8080/Struts/login/login.jsp. Fill you name and password in the form, you can see it works.