今天写个简单的登录应用。就是在没有用户登录进去时,主页显示Welcome world,当用户成功登录进去后,显示Welcome Username,同时出现一些只有登录用户才能看到的内容。与此同时还有一个Logout供已登录的用户登出。
需要的东西仍旧是我第一篇Struts例子里面的:jdk1.5, Tomcat 5.5, Eclipse3.2, Struts1.2包,common包.
LoginAction, LoginForm, LogoutAction,ApplicationResource都放在src下面,index.jsp, success.jsp, failure.jsp放在根目录下面,web.xml, struts-config.xml, validation.xml, validator-rules.xml放在WEB-INF下。
首先,我们配置web.xml文件。这个跟之前的例子差不多。
web.xml:
<?
xml version="1.0" encoding="ISO-8859-1"?><!
DOCTYPE web-appPUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN""http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"
><
web-app> <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>0</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> </taglib></
web-app>这里又用到了struts的logic和bean标签。
然后是index.jsp文件,这个就跟之前的有很大不同了。
index.jsp:
<%@
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@
taglib uri="/tags/struts-html" prefix="html" %><%@
taglib uri="/tags/struts-bean" prefix="bean" %><%@
taglib uri="/tags/struts-logic" prefix="logic" %>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><
html><
head><
meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><
title>Index Page</title></
head><
body> <center> <logic:present name="User">Welcome!
<bean:write name="User" property="username" /><br/> <a href="success.jsp">Go to success page</a><br/> </logic:present> <logic:notPresent name="User">Welcome world!
<br/> <html:form. action="/login.do" method="post"> <table> <tr> <td>Username:<html:text property="username" /></td> <td><html:errors property="username" /></td> </tr> <tr> <td>Password:<html:password property="password" /></td> <td><html:errors property="password" /></td> <tr> <tr> <td align="center"><html:submit value="Login" /></td> </tr> </table> </html:form> </logic:notPresent> <br/> <logic:present name="User"> <html:link forward="logout">Logout</html:link> </logic:present> </center></
body></
html>我们在这里用了<logic:present>标签。它的意思就是当它以name指定的bean出现时就显示里面的内容,如果没有出现就不显示。而<logic:notPresent>就刚好相反。不存在时显示,存在时不显示。
<bean:write>标签则是显示以name指定的bean的property内容。
<html:link forward>标签则提供了一个Logout的forward。
下面我们来看看struts-config.xml:
<?
xml version="1.0" encoding="UTF-8"?><!
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-sources /> <form-beans> <form-bean name="loginForm" type="src.LoginForm" /> </form-beans> <global-exceptions /> <global-forwards> <forward name="logout" path="/logout.do" /> </global-forwards> <action-mappings> <action path="/login" name="loginForm" input="/index.jsp" validate="true" type="src.LoginAction" > <forward name="success" path="/success.jsp" /> <forward name="failure" path="/failure.jsp" /> </action> <action path="/logout" type="src.LogoutAction" > <forward name="logout" path="/index.jsp" /> </action> </action-mappings> <message-resources parameter="src.ApplicationResource" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validation.xml,/WEB-INF/validator-rules.xml" /> </plug-in></
struts-config> 这里,global-forwards里面多了一个logout,就是处理点击logout后要转向的action。然后在下面的action-mappings里面配置了由LogoutAction来处理这个请求。我们还是使用了validator框架。需要将validator-rules.xml和validation.xml放在WEB-INF下面。可以参见前面的一篇Validator例子。
validation.xml:
<?
xml version="1.0" encoding="UTF-8"?><!
DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"><
form-validation> <formset> <form. name="loginForm"> <field property="username" depends="required"> <arg key="prompt.username" /> </field> <field property="password" depends="required,minlength"> <arg0 key="prompt.password" /> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <var> <var-name>minlength</var-name> <var-value>5</var-value> </var> </field> </form> </formset></
form-validation>ApplicationResource.properties:
prompt.username=
User Nameprompt.password=
User Passworderrors.required=
{0} is required!errors.minlength=
{0} can't be less than {1} characters!然后是LoginForm.java。我们这里只用到了username和password两个属性,同时又使用了validator,所以不用编写validate()方法了。
LoginForm.java:
package
src;import
org.apache.struts.validator.ValidatorForm; public class LoginForm. extends ValidatorForm{ private String username; private String password; public String getUsername() { return username;}
public void setUsername(String username) { this.username = username;}
public String getPassword() { return password;}
public void setPassword(String password) { this.password = password;}
}
然后是LoginAction.java。在这里面,当输入的用户名和密码正确后(Jim, 1234567),便会将一个名为User的bean放入session,说明用户已登录。
LoginAction.java:
package
src;import
javax.servlet.http.*;import
org.apache.struts.action.*;public
class LoginAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm. form,
HttpServletRequest request,
HttpServletResponse response){
LoginForm. lf = (LoginForm)form;
HttpSession session = request.getSession();
String username = lf.getUsername();
String password = lf.getPassword();
ActionForward forward;
if(username.equals("Jim")&& password.equals("1234567")){session.setAttribute(
"User", lf);forward = mapping.findForward(
"success");}
else{forward = mapping.findForward(
"failure");}
return forward;}
}
接着编写success.jsp,里面也用到了<logic:present>和<bean:write>标签:
<%@
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@
taglib uri="/tags/struts-html" prefix="html" %><%@
taglib uri="/tags/struts-bean" prefix="bean" %><%@
taglib uri="/tags/struts-logic" prefix="logic" %><!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><
html><
head><
meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><
title>Success</title></
head><
body><
center><
logic:present name="User">Welcome!
<bean:write name="User" property="username" /></
logic:present><
br/>You can review your interested targets here!
<br/><
a href="index.jsp">Return to index page</a></
center></
body></
html>如果登录失败,要有一个失败的页面,即failure.jsp:
<%@
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><
html><
head><
meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><
title>Failure</title></
head><
body><
center><
b>Login Failed!</b><br/><
a href="index.jsp">Return to index page</a></
center></
body></
html>现在还差一个LogoutAction.java了:
package
src;import
javax.servlet.http.*;import
org.apache.struts.action.*;public
class LogoutAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm. form,
HttpServletRequest request,
HttpServletResponse response){
ActionForward forward =
new ActionForward();HttpSession session = request.getSession();
session.invalidate();
//session.removeAttribute("User");forward = mapping.findForward(
"logout"); return forward;}
}
这里,我用了session.invalidate()方法,它的作用是消除一切session的内容。而removeAttribute()则只是将指定的bean移出线程。用后者的话,当登出返回登录页面时,以前的username和password还会在输入框里面。所以我用了invalidate()方法。这样的话登出后username和password输入框会被清空。
好了,现在可以运行看看效果了。这个例子比较简单,实现了最简单的登录登出功能。我们可以根据这个在以后的程序中添加更多的功能来丰富内容。