<%@ page contentType="text/html; charset=gb2312"%> Struts入门
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
Struts入门
     发布者: 发布时间:2007-02-10
Struts学习傻瓜式入门篇
或许有人觉得struts不容易学,似乎里面的一些概念让未接触过的人迷惑,MVC1、MVC2、模式……我写这篇文章是想让从来没有接触过struts的人,能有个简单的入门指引,当然,系统地学习struts是必要的,里面有很多让人心醉的东东,那是后话了。

  该案例包括首页,用户登陆、网站向导页面。就这么简单,没有深奥的struts概念,主要靠动手,然后用心体会。

  WEB Server用tomcat4。到http://jakarta.apache.org下载struts1.1,把zip文件释放到c:struts,拷贝C:strutswebappsstruts-example.war到c: omcat4webapps中,启动tomcat,war包被释放为struts-example文件夹,删除war包,把struts-example文件夹更名为test。
  一、把WEB-INFweb.xml改成:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<!?这是struts中的Controller(控制器),系统的指令中转由其,既ActionServlet 类负责,它从struts-config.xml中读取配置信息,并在服务器后台自动启动一个线程。如果没有特别的要求(如添加语言编转功能),程序员可以不管这部分,照用就可以了。-->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<!--该系统的servlet可以映射成cool为后缀的文件,而不是常见的.jspdo等,后缀名可以改成任何名称,当然名字要健康#◎¥%!-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.cool</url-pattern>
</servlet-mapping>
<!--该系统的默认首页是index.jsp,可以有多个,系统按次序找,类似IIS-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

  二、把testWEB-INF struts-config.xml改成:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<!--FormBean是struts的一个概念,本质是JavaBean,用来自动存储页面表单中各个域的值,并在适当的时候回填表单域,不需要象传统那样request.getParameter(“fieldName”);,常被action-mappings中的action 使用-->
<form-beans>
<!?稍后我们会新增一个UserForm类,用来存储用户信息。-->
<form-bean name="userForm" type="test.UserForm"/>
</form-beans>
<!--这里存放整个系统都可以使用的全局转向中转(Forward)地址,类似于javascript中的window.location(‘index.jsp’);,也类似于电视控制器上的各种按钮,可以转频道、调色等等是基于Struts的Web应用的控制流程流转。一般情况下,一个Action处理完毕后,会转发到一个JSP页面进行显示。这也是JSP中的MVC的实现的要点。-->
<global-forwards>
<!--failed.cool将被当成servlet请求,到action-mappings中寻找对应的action处理。-->
<forward name="failed" path="/failed.cool"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<!--还记得web.xml中后缀为cool的请求吗?它们是转到这里处理的。这里相当于struts的Model部分,Model部分是struts中比较灵活的地方。-->
<action-mappings>
<!--处理regist.cools的请求,使用的FormBean是userForm,既test.UserForm类,当处理过程发生错误时将返回index.jsp-->
<action path="/regist" type="test.RegistAction" name="userForm" scope="request" input="/index.jsp" />
<action path="/overview" forward="/hello.jsp"/>
<action path="/failed" forward="/wuwu.jsp" />
</action-mappings>
</struts-config>
  三、增加一个FormBean,类路径为test.UserForm,以下是这个类的内容:

package test;
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm
{
 private String name="lpw";//用户名
 private String ps="1111";//密码
 public UserForm(){}
 public void setName(String s) {name=s;}
 public String getName() {return name;}
 public void setPs(String s) {ps=s;}
 public String getPs() {return ps;}
}


  四、增加一个Action的子类,类路径为test. RegistAction,以下是这个类的内容:

package test;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import test.UserForm;
public final class RegistAction extends Action
{
 public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response)
 throws Exception
 {
  Locale locale = getLocale(request);
  MessageResources messages = getResources(request);
  HttpSession session = request.getSession();
  UserForm userform = (UserForm) form;
  //此处可以调用其他类来执行数据库写入或其他逻辑判断
  // 如果UserForm传来的参数name的值为默认的lpw,将forward到failed,
  // 该名称将到struts-config.xml的<global-forwards>中寻找映射的url地址
  // (可以是绝对路径,也可以是相对路径),对于本例,是转到failed.cool,
  // 还记得吗?后缀为cool的请求全部到action-mappings中寻找
  // 对应的action处理,最终目录是wuwu.jsp*/
  if( "lpw".equals(userform.getName()) )
   return (mapping.findForward("failed"));
  else
   return (mapping.findForward("regist"));
 }
}

  五、以下所有新增或修改的页面相当于struts的View部分,把首页index.jsp改成:

<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ page import = "test.*" %>
<a href="overview.cool">站点导航</a><br>
<form action="regist.cool" method="post">
<!?表单中的域的名称要和UserForm中的参数一样,就可以实现数据自动获取功能,不需要用request.getParameter(“param”);-->
用户:<input type="text" name="name"><br>
密码:<input type="password" name="ps"><br>
<input type=submit value="新增用户">
</form>

  六、增加hello.jsp,用于站点导航:

<h1>site map</h1>The following is content filling by reader

  七、增加wuwu.jsp,当没有新用户登陆时,将转到这个页面:

<%@ page contentType="text/html;charset=GBK" language="java" %>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
现有用户:<%=beanlpw.getName()%><br>
密码:<%=beanlpw.getPs()%><br>

  没有得到新的用户!

  八、增加regist.jsp,当有新用户登陆时,将转到这个页面:

<%@ page contentType="text/html;charset=GBK" language="java" %>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
新用户帐号:<%=beanlpw.getName()%><br>
密码:<%=beanlpw.getPs()%>

  九、启动tomcat4,浏览器中键入http://localhost:8080/test/index.jsp,操作一下,就可以看到结果,并初步理解struts的M、V、C各部分的协同工作原理,当然这是作者的良好意愿,如果读者看得一头雾水,欢迎指出错误在哪里 :)
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
浅谈4种类型的JDBC驱动程序
servlet+bean+jsp综合实例,希望能给你提示!!
mvc例子
JSTL(JSP标准标签库)介绍
在Java中向Excel文件写入内容
在Java中读取Excel文件的内容
 
最近评论:
        
Chris
1e2709c8ac455640be315d64912d8321 http://villagio-palumbo.kajgdw.biz/ http://maria-amorelli-roma.tzlnou.biz/ http://premio-sapio-palermo.ygvhik.biz/ http://allevamento-di-bolognese.ygvhik.biz/ http://cassa-edile-verona.zibtye.biz/ http://amministrazioni-immobiliare.zibtye.biz/ http://agenzia-viaggio-offerta-lavoro-direttori-tecnico.enadzh.biz/ http://milly-d-abbraccio-proposta-oscena.enadzh.biz/ http://maschera-ferro-caprio.zibtye.biz/ http://samsung-sgh-z500-prezzo.tzlnou.biz/ 69fae163d26a9b1682339a4eb6fc4ad9
        
Forrest
77a069a3360ae213301d474a1829022a http://foto-impianto-industriali.mnkcbe.org/ http://s-angelo-dei-lomb.pvaeyo.org/ http://licenziamento-portiera-and-sentenza.gbdrme.org/ http://legge-398-del-16-12-1991.mnkcbe.org/ http://raccontare-cosa-volontariato-ai-bambino.mnkcbe.org/ http://case-in-affitto-san-benedetto-po.cqhnnx.org/ http://bambola-ramona-foto-gallery.pvaeyo.org/ http://universita-politecnica-marche-ingegneria.mnkcbe.org/ http://trucchi-warcraft-3-reign.pvaeyo.org/ http://tavolini-da-salotto-in-legno.cqhnnx.org/ eb89aa2351bfb8dd061b0dc25061dcdb
        
Rohan
6b4d52380b40249a150f079207efd904 JorgeDuhn WineingerKade GuadalupeBurdge BlaineKlick BraedonTorrens LeonardoChachere HowardMar PrinceHelsley AaronCeleste ArakiElian 1bb5907b91a875f331998f8e4f2b0572
        
Dwight
a91e1d34ceea0899ae2fc8f7adadfd0f team rinascita romagna contabilita centro costo progettista meccanico sei parte zero assoluto mp3 gratis ottimizzazione riscaldamento domestico gta san andreas da scaricare gratis video tutto e possibile sito web sicilia ordini birra immagine personaggio topolino b8fb7d84153cc5c69600cbe1497734b2
        
Bryan
847e5dbadae3d6170f92f45fee61cb20 http://meteo-portico.uylqdg.com/ http://aerei-romania.licoxi.in/ http://garanzia-batterie.uylqdg.com/ http://sesso-orale-amatoriale.aoknmm.in/ http://cappotto-cachemire.licoxi.in/ http://mollusco-balena-bologna.kvpzig.com/ http://museo-inglesi.aoknmm.in/ http://introduzione-alla-musica.iznvge.in/ http://precedenti-taranto-perugia.fzhoas.in/ http://soggetto-non-residente.aoknmm.in/ b8a12f78e2ab8d9c8e5e94f78e975725
        
Marshall
73d55900b105b73ba72167efc7f12c2c ragazza immagine lago garda brescia carla elvira lucia dall oglio gara servizio agricoltura architettura scuola modulo x petizione salute scaricare agriturismo cerreto reggio emilia futuro giovani diventare medico militare atm milano mappa satelliti della terra 8ea4fcdde1a965ef95e68187f350c6f6
        
Jalen
http://83deec3f019dae5a9c268b3ca1f8673b-t.xkktxb.org 83deec3f019dae5a9c268b3ca1f8673b [url]http://83deec3f019dae5a9c268b3ca1f8673b-b1.xkktxb.org[/url] [url=http://83deec3f019dae5a9c268b3ca1f8673b-b2.xkktxb.org]83deec3f019dae5a9c268b3ca1f8673b[/url] [u]http://83deec3f019dae5a9c268b3ca1f8673b-b3.xkktxb.org[/u] 8d1f2bfe3cbc5359328d95464cab8b7c
        
Bernardo
2cead58db4f53f07a89ecd4ce1bd5e5b http://enel-assunzione-sardegna.fuypfr.biz/ http://nokia-6100-cavo.mxkrxs.com/ http://pamela-anderson-nuda-it.knhtou.com/ http://educazione-donne.nioqlj.com/ http://medicina-terni.jmncsw.biz/ http://laser-gamba-gonfie.fuypfr.biz/ http://ragazzino-presa-bocca.fuypfr.biz/ http://parafrasi-canto-notturno-pastore-errante-asia.xsixxz.biz/ http://24-ore-spa-1975.xsixxz.biz/ http://cremona-calcio-femminile.nioqlj.com/ 8cff813cd5cdf93d908a9e43c4704dad
        
Andres
f829c00edcf8706c12aa7b3605b994e5 roma mercatino natale heidelberg sintesi famiglia dell antiquario emanuela talento hotel ischia pasqua castello konigstein d angelo daniele roseto cucina suor germana vestito laurea stampante laser a3 monocromatica marsiglia formula 1 marseille a875aa102e91579b074fe29fa7a13e81
        
Jeremy
01caaf98ee7aa17af4e20bad985ad607 http://alberto-monaco-fidanzata.lldpzx.org/ http://cenone-puglia-capodanno.lldpzx.org/ http://classe-250.lvqits.org/ http://america-gianna-nannini.gazdtl.org/ http://acea-ato-5.wuzzme.org/ http://foce-dell-amur.gazdtl.org/ http://biglietto-treno-scontato.mhjqva.org/ http://pullman-www-gita.iwfpha.org/ http://telescopio-guida.hihuft.org/ http://poesie-primavera.ubetii.org/ 8c2a5fabd273020cebfaea52010ee4bb
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号