struts实例

这只是我的工作总结,其实也算是刚刚搞J2EE吧,所以还算是个新手,可从来都懒的写文章的,现在回头想象总结原来是如此的重要,本文主要描述的是eclipse下整合struts。其实网上这样的文章不知道有多少,但还是希望有兴趣的朋友不仿看看,要是发现有不好的地方的话恳请指正,谢谢!(环境:windows xp+tomcat5.5.28+jdk6+eclipse3.3+myeclipse6)

1 打开eclipse,新建web project。

struts实例

2 整合struts,右击项目选择myeclipse下的add struts Capabilities。

struts实例

3 创建一个名为LoginActionForm的ActionForm文件,如下图:

struts实例

LoginActionForm.java 源代码如下:

/***/ /**
*Copyright2008,CSSWEBallrightsreserved.
*
@authorhujun
*@dateMar21,2008
*@fileLoginActionForm.java
*
@version1.1
*
*/

package css.web.orc.form;

import org.apache.struts.action.ActionForm;

public class LoginActionForm extends ActionForm ... {

privatestaticfinallongserialVersionUID=1L;

privateStringuserid;
privateStringpasswd;
privateStringmsg;

publicStringgetUserid()...{
returnuserid;
}

publicvoidsetUserid(Stringuserid)...{
this.userid=userid;
}

publicStringgetPasswd()...{
returnpasswd;
}

publicvoidsetPasswd(Stringpasswd)...{
this.passwd=passwd;
}

publicStringgetMsg()...{
returnmsg;
}

publicvoidsetMsg(Stringmsg)...{
this.msg=msg;
}

}

在init下创建一个名为LoginAction的Action文件,如下:

/***/ /**
*Copyright2008,CSSWEBallrightsreserved.
*
@authorhujun
*@dateMar21,2008
*@fileLoginAction.java
*
@version1.1
*
*/

package css.web.orc.action.init;

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;

import css.web.orc.form.LoginActionForm;

public class LoginAction extends Action ... {

publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
...{
Stringforward
="";
try
...{
LoginActionFormloginform
=(LoginActionForm)form;
Stringuserid
="";
Stringpasswd
="";
userid
=loginform.getUserid();
passwd
=loginform.getPasswd();
if(userid.equals("hujun")&&passwd.equals("123456"))...{
forward
="main";
}

else...{
forward
="login";
loginform.setMsg(
"您输入的用户名和密码不正确!");
}

}

catch(Exceptione)...{
e.printStackTrace();
}

returnmapping.findForward(forward);
}

}

4 接下来要在webroot下建两个.jsp文件,分别是login.jsp和main.jsp,代码如下:

<% ... @pagelanguage="java" %>
<% ... @pagecontentType="text/html;charset=GBK" %>

<% ... @tagliburi="/WEB-INF/struts-bean.tld"prefix="bean" %>
<% ... @tagliburi="/WEB-INF/struts-html.tld"prefix="html" %>
<% ... @tagliburi="/WEB-INF/struts-logic.tld"prefix="logic" %>

< html >
< title > 首页 </ title >
< body >
< html:form action ="/init/LoginAction.do" method ="post" >
< br >< br >< br >< br >< br >< br >
< table width ="50%" align ="center" border ="0" >
< tr >
< td width ="100%" align ="left" >
< font size ="2" color ="red" >
< b >< bean:write name ="LoginActionForm" property ="msg" /></ b >
</ font ></ td >
</ tr >
</ table >
< table width ="50%" align ="center" border ="1" >
< tr >
< td width ="30%" align ="right" > 用户名: </ td >
< td width ="70%" align ="left" >
< html:text property ="userid" maxlength ="15" ></ html:text >
</ td >
</ tr >
< tr >
< td width ="30%" align ="right" > 密码: </ td >
< td width ="70%" align ="left" >
< html:text property ="passwd" maxlength ="30" ></ html:text >
</ td >
</ tr >
</ table >
< table width ="50%" align ="center" border ="0" >
< tr >
< td width ="50%" align ="right" >< html:submit value ="确认" /></ td >
< td width ="50%" align ="left" > &nbsp;&nbsp;&nbsp;&nbsp; < html:reset value ="重置" /></ td >
</ tr >
</ table >
</ html:form >
</ body >
</ html >
<% ... @pagelanguage="java" %>
<% ... @pagecontentType="text/html;charset=GBK" %>

<% ... @tagliburi="/WEB-INF/struts-bean.tld"prefix="bean" %>
<% ... @tagliburi="/WEB-INF/struts-html.tld"prefix="html" %>
<% ... @tagliburi="/WEB-INF/struts-logic.tld"prefix="logic" %>

< html >
< title > 首页 </ title >
< body >
< html:form action ="/init/LoginAction" method ="post" >
< br >< br >< br >< br >< br >
< table width ="50%" align ="center" border ="0" >
< tr >
< td width ="100%" align ="left" >< font size ="5" color ="blue" >< b >
欢迎您,
< bean:write name ="LoginActionForm" property ="userid" ></ bean:write >
</ b ></ font ></ td >
</ tr >
</ table >
</ html:form >
</ body >
</ html >

5 web.xml文件如下:

<? xmlversion="1.0"encoding="UTF-8" ?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< 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 > 3 </ param-value >
</ init-param >
< init-param >
< param-name > detail </ param-name >
< param-value > 3 </ param-value >
</ init-param >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > action </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file > /init/login.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >

6 struts-config.xml文件如下:

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >

< struts-config >
< form-beans >
< form-bean name ="LoginActionForm" type ="css.web.orc.form.LoginActionForm" ></ form-bean >
</ form-beans >

< global-forwards >
< forward name ="error" path ="init/errPage.jsp" ></ forward >
</ global-forwards >

< action-mappings >
< action name ="LoginActionForm" type ="css.web.orc.action.init.LoginAction" scope ="request" path ="/init/LoginAction" >
< forward name ="main" path ="/init/main.jsp" ></ forward >
< forward name ="login" path ="/init/login.jsp" ></ forward >
</ action >
</ action-mappings >
</ struts-config >

7 将项目部署到tomcat下:

struts实例

struts实例

struts实例

8 好了,接下来运行tomcat,然后在浏览器里输入地址,运行即可。

你可能感兴趣的:(struts)