这只是我的工作总结,其实也算是刚刚搞J2EE吧,所以还算是个新手,可从来都懒的写文章的,现在回头想象总结原来是如此的重要, 本文主要描述的是eclipse下整合struts。其实网上这样的文章不知道有多少,但还是希望有兴趣的朋友不仿看看,要是发现有不好的地方的话恳请指正,谢谢!(环境:windows xp+tomcat5.5.28+jdk6+eclipse3.3+myeclipse6)
1 打开eclipse,新建web project。
2 整合struts,右击项目选择myeclipse下的add struts Capabilities。
3 创建一个名为LoginActionForm的ActionForm文件,如下图:
LoginActionForm.java 源代码如下:
/** */
/**
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 21, 2008
*@file LoginActionForm.java
*@version 1.1
**/
package css.web.orc.form;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm ... {
private static final long serialVersionUID = 1L;
private String userid;
private String passwd;
private String msg;
public String getUserid() ...{
return userid;
}
public void setUserid(String userid) ...{
this.userid = userid;
}
public String getPasswd() ...{
return passwd;
}
public void setPasswd(String passwd) ...{
this.passwd = passwd;
}
public String getMsg() ...{
return msg;
}
public void setMsg(String msg) ...{
this.msg = msg;
}
}
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 21, 2008
*@file LoginActionForm.java
*@version 1.1
**/
package css.web.orc.form;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm ... {
private static final long serialVersionUID = 1L;
private String userid;
private String passwd;
private String msg;
public String getUserid() ...{
return userid;
}
public void setUserid(String userid) ...{
this.userid = userid;
}
public String getPasswd() ...{
return passwd;
}
public void setPasswd(String passwd) ...{
this.passwd = passwd;
}
public String getMsg() ...{
return msg;
}
public void setMsg(String msg) ...{
this.msg = msg;
}
}
在init下创建一个名为LoginAction的Action文件,如下:
/** */
/**
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 21, 2008
*@file LoginAction.java
*@version 1.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 ... {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) ...{
String forward = "";
try
...{
LoginActionForm loginform = (LoginActionForm)form;
String userid = "";
String passwd = "";
userid = loginform.getUserid();
passwd = loginform.getPasswd();
if(userid.equals("hujun")&&passwd.equals("123456")) ...{
forward = "main";
}
else ...{
forward = "login";
loginform.setMsg("您输入的用户名和密码不正确!");
}
}
catch (Exception e) ...{
e.printStackTrace();
}
return mapping.findForward(forward);
}
}
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 21, 2008
*@file LoginAction.java
*@version 1.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 ... {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) ...{
String forward = "";
try
...{
LoginActionForm loginform = (LoginActionForm)form;
String userid = "";
String passwd = "";
userid = loginform.getUserid();
passwd = loginform.getPasswd();
if(userid.equals("hujun")&&passwd.equals("123456")) ...{
forward = "main";
}
else ...{
forward = "login";
loginform.setMsg("您输入的用户名和密码不正确!");
}
}
catch (Exception e) ...{
e.printStackTrace();
}
return mapping.findForward(forward);
}
}
4 接下来要在webroot下建两个.jsp文件,分别是login.jsp和main.jsp,代码如下:
<%
...
@ page language="java"
%>
<% ... @ page contentType="text/html;charset=GBK" %>
<% ... @ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<% ... @ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<% ... @ taglib uri="/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" > < html:reset value ="重置" /></ td >
</ tr >
</ table >
</ html:form >
</ body >
</ html >
<% ... @ page contentType="text/html;charset=GBK" %>
<% ... @ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<% ... @ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<% ... @ taglib uri="/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" > < html:reset value ="重置" /></ td >
</ tr >
</ table >
</ html:form >
</ body >
</ html >
<%
...
@ page language="java"
%>
<% ... @ page contentType="text/html;charset=GBK" %>
<% ... @ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<% ... @ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<% ... @ taglib uri="/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 >
<% ... @ page contentType="text/html;charset=GBK" %>
<% ... @ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<% ... @ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<% ... @ taglib uri="/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文件如下:
<?
xml version="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/javaee http://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 >
< 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/javaee http://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文件如下:
<?
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 >
< 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 >
<! 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 >
< 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下:
8 好了,接下来运行tomcat,然后在浏览器里输入地址,运行即可。