Struts+Spring+Hibernate是三大主流框架,目前正在学习中。想分别写三个框架的简单例子,再写个综合应用的。Hibernate的已经有了,今天写个Struts的。很简单的,就是登录验证,若Username为"Jim",Password为"123"就显示Success!,错误就显示Failure!.同时还有输入不能为空的验证。
需要的准备:jdk1.5, Eclipse3.2, Tomcat 5.5, Struts 1.2包, common包。将struts的tld文件放在WEB-INF的tld文件夹里面(这个文件夹要自己建)。
新建一个Web工程,取名为StrutsTest.
编写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></
web-app>其中,taglib就是指定你要用到的tld文件的地址。taglib-uri则是在jsp页面中你引用这些文件的标示。
然后,在WebContent下面写一个index.jsp:
<%@
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@
taglib uri="/tags/struts-html" prefix="html" %>
<!
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> <b>Welcome!</b> <html:form. action="/login.do" method="post">Username:
<html:text property="username" /><br/>Password:
<html:password property="password" /><br/> <html:submit value="Submit" /> <html:reset value="Reset" /><br/> <html:errors/> </html:form></
center></
body></
html>这里用到了struts的html标签,在该jsp页面的开始声明了要用到这个标签。有了struts标签,我们可以很简单地构建一个form。
注意:form里面的action一定要以"/"开头,结尾要有.do,因为我们在web.xml里面定义了url-pattern的。要按照这个来写。
然后写struts-config.xml,跟web.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 /> <action-mappings> <action path="/login" name="loginForm" input="/index.jsp" type="src.LoginAction" > <forward name="success" path="/success.jsp" /> <forward name="failure" path="/failure.jsp" /> </action> </action-mappings> <message-resources parameter="src.ApplicationResource" /></
struts-config>注意:在这个配置文件里面,属性的位置顺序不要弄错了。就是说,一定要按照<data-source>,<form-beans>,<global-exceptions>,<global-forwards>,<action-mappings>这样的顺序来写,不然不符合struts-config.xml的规范。
还有就是,action属性里面,path要以"/"开头,而且后面不能加".do"了,因为提交表单的时候会自动忽略后缀,而这个后缀我们已经在web.xml里面定义过了。还有input也要以"/"开头。
然后在src下面写一个LoginForm.java:
package
src;import
javax.servlet.http.*;import
org.apache.struts.action.*;public
class LoginForm. extends ActionForm{ private String username; private String password; public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ActionErrors errors =
new ActionErrors(); if(username==null||username.length()<1){errors.add(
}
if(password==null||password.length()<1){errors.add(
}
return errors;}
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:
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;
String username = lf.getUsername();
String password = lf.getPassword();
ActionForward forward;
if(username.equals("Jim")&& password.equals("123")){forward = mapping.findForward(
"success");}
else{forward = mapping.findForward(
"failure");}
return forward;}
}
最后,不要忘记写你的资源文件,在struts-config.xml里面的<message-resources>定义了的,文件全称是ApplicationResource.properties。它是用来显示你的错误消息的。放的位置也在src里面:
errors.username.required =
Username can't be null!<br/>errors.password.required =
Password can't be null!<br/>还有不要忘了写success.jsp和failure.jsp,放的位置跟index.jsp相同。
success.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>Success</title></
head><
body><
center><
b>Success!</b><br/><
a href="index.jsp">Return</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>Failure!</b><br/><
a href="index.jsp">Return</a></
center></
body></
html>好了,一个简单的Struts例子完成了。大家可以执行看看。当然,这个例子的确很简单,没有将Struts的所有方面展示出来。以后再慢慢添加其它功能。