struts标签学习(低版本)

为了便于理解和记忆整个过程,将学过的东西贴过来。项目整体结构:

java代码如下:

ActionForm负责对数据的包装。

Code:
  1. /*
  2. *GeneratedbyMyEclipseStruts
  3. *Templatepath:templates/java/JavaClass.vtl
  4. */
  5. packageprj30.form;
  6. importjavax.servlet.http.HttpServletRequest;
  7. importorg.apache.struts.action.ActionErrors;
  8. importorg.apache.struts.action.ActionForm;
  9. importorg.apache.struts.action.ActionMapping;
  10. /**
  11. *MyEclipseStruts
  12. *Creationdate:01-26-2010
  13. *
  14. *XDocletdefinition:
  15. *@struts.formname="regForm"
  16. */
  17. publicclassRegFormextendsActionForm{
  18. privateStringaccount;
  19. privateStringpassword;
  20. privateStringinfo;
  21. privateStringhiddenInfo;
  22. privateStringsex="girl";//可以通过ActionForm来设置表单的初始值。
  23. privateStringhome;
  24. privateString[]fav={"sing"};//可以用数组属性来存储传过来的多个同名表单元素。
  25. privatebooleaninterm;
  26. privateString[]books={"struts","hibernate"};
  27. publicString[]getBooks(){
  28. returnbooks;
  29. }
  30. publicvoidsetBooks(String[]books){
  31. this.books=books;
  32. }
  33. publicbooleanisInterm(){
  34. returninterm;
  35. }
  36. publicvoidsetInterm(booleaninterm){
  37. this.interm=interm;
  38. }
  39. publicString[]getFav(){
  40. returnfav;
  41. }
  42. publicvoidsetFav(String[]fav){
  43. this.fav=fav;
  44. }
  45. publicStringgetHome(){
  46. returnhome;
  47. }
  48. publicvoidsetHome(Stringhome){
  49. this.home=home;
  50. }
  51. publicStringgetSex(){
  52. returnsex;
  53. }
  54. publicvoidsetSex(Stringsex){
  55. this.sex=sex;
  56. }
  57. publicStringgetHiddenInfo(){
  58. returnhiddenInfo;
  59. }
  60. publicvoidsetHiddenInfo(StringhiddenInfo){
  61. this.hiddenInfo=hiddenInfo;
  62. }
  63. publicStringgetPassword(){
  64. returnpassword;
  65. }
  66. publicvoidsetPassword(Stringpassword){
  67. this.password=password;
  68. }
  69. publicStringgetInfo(){
  70. returninfo;
  71. }
  72. publicvoidsetInfo(Stringinfo){
  73. this.info=info;
  74. }
  75. publicStringgetAccount(){
  76. returnaccount;
  77. }
  78. publicvoidsetAccount(Stringaccount){
  79. this.account=account;
  80. }
  81. publicActionErrorsvalidate(ActionMappingmapping,
  82. HttpServletRequestrequest){
  83. returnnull;
  84. }
  85. publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){
  86. //该函数是每次提交时都会调用的重新设置interm的值
  87. this.interm=false;
  88. }
  89. }

Action负责对数据的处理和jsp界面的跳转:

Code:
  1. /*
  2. *GeneratedbyMyEclipseStruts
  3. *Templatepath:templates/java/JavaClass.vtl
  4. */
  5. packageprj30.action;
  6. importjavax.servlet.http.HttpServletRequest;
  7. importjavax.servlet.http.HttpServletResponse;
  8. importorg.apache.struts.action.Action;
  9. importorg.apache.struts.action.ActionForm;
  10. importorg.apache.struts.action.ActionForward;
  11. importorg.apache.struts.action.ActionMapping;
  12. importprj30.form.RegForm;
  13. publicclassRegActionextendsAction{
  14. publicActionForwardexecute(ActionMappingmapping,ActionFormform,
  15. HttpServletRequestrequest,HttpServletResponseresponse){
  16. RegFormregForm=(RegForm)form;//TODOAuto-generatedmethodstub
  17. request.setAttribute("regForm",regForm);
  18. returnnewActionForward("/regResult.jsp");
  19. }
  20. }

哎,郁闷怎么没有jsp的代码格式呢!

为了对struts标签的学习,故写了一个注册表单界面和一个注册结果的界面。

Code:
  1. <%@pagelanguage="java"pageEncoding="gb2312"%>
  2. <%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
  3. <%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
  4. <html>
  5. <head>
  6. <title>JSPforRegFormform</title>
  7. </head>
  8. <body>
  9. <html:formaction="/reg"method="post">
  10. <!--property是元素的名称。-->
  11. 请您输入账号(文本框):<html:textproperty="account"></html:text><BR>
  12. 请输入您的密码(密码框):<html:passwordproperty="password"></html:password>
  13. <br>
  14. <!--注意:在Struts标签中,系统看到某个property,如果没有手动设置value,
  15. 就是调用个ActionFrom的get方法来设置初始值-->
  16. 请输入您的个人信息(多行文本框):<br>
  17. <html:textareaproperty="info"rows="6"cols="40"></html:textarea>
  18. <br><html:hiddenproperty="hiddenInfo"value="welcome爱你一万年"></html:hidden>
  19. <br>
  20. 请选择性别(单选按钮):<html:radioproperty="sex"value="boy"></html:radio>
  21. <html:radioproperty="sex"value="girl"></html:radio><br>
  22. 籍贯(下拉菜单):<html:selectproperty="home"value="lfjieshi">
  23. <html:optionvalue="lfjieshi">陆丰碣石</html:option>
  24. <html:optionvalue="lfdonghai">陆丰东海</html:option>
  25. <html:optionvalue="lfjiazi">陆丰甲子</html:option>
  26. </html:select><BR>
  27. 爱好(多项复选框):
  28. <html:multiboxproperty="fav"value="sing"></html:multibox>唱歌
  29. <html:multiboxproperty="fav"value="swim"></html:multibox>游泳
  30. <html:multiboxproperty="fav"value="playgame"></html:multibox>
  31. <html:multiboxproperty="fav"value="dance">跳舞</html:multibox><br>
  32. 是否会员(单选复选框):
  33. <html:checkboxproperty="interm"></html:checkbox><br>
  34. 选择您喜欢的书本(多选下拉类表框):
  35. <html:selectproperty="books"multiple="true"size="6">
  36. <html:optionvalue="database">数据库</html:option>
  37. <html:optionvalue="struts">struts框架</html:option>
  38. <html:optionvalue="spring">spring框架</html:option>
  39. <html:optionvalue="hibernate">hibernate框架</html:option>
  40. </html:select>
  41. <html:submitvalue="提交注册信息"></html:submit>
  42. </html:form>
  43. </body>
  44. </html>
Code:
  1. <%@pagelanguage="java"pageEncoding="gb2312"%>
  2. <%@pageimport="prj30.form.RegForm"%>
  3. <%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
  4. <%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
  5. <html>
  6. <head>
  7. <title>注册处理结果界面</title>
  8. </head>
  9. <body>
  10. <%RegFormregForm=(RegForm)request.getAttribute("regForm");%>
  11. 账号:<%=regForm.getAccount()%><br>
  12. 密码:<%=regForm.getPassword()%><br>
  13. 个人信息:<%=regForm.getInfo()%><br>
  14. 隐藏的表单is:<%=regForm.getHiddenInfo()%><BR>
  15. 性别:<%=regForm.getSex()%><BR>
  16. 籍贯:<%=regForm.getHome()%><BR>
  17. 爱好:<%
  18. String[]fav=regForm.getFav();
  19. for(inti=0;i<fav.length;i++){
  20. out.println(fav[i]);
  21. }
  22. %><br>
  23. 是否会员:<%=regForm.isInterm()%><br>
  24. 选择的书本是:<%
  25. String[]books=regForm.getBooks();
  26. for(inti=0;i<books.length;i++){
  27. out.println(books[i]);
  28. }%>
  29. </body>
  30. </html>

配置文件是直接使用图形界面操作的,代码贴下来有空也可以研究研究

Code:
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd">
  3. <struts-config>
  4. <form-beans>
  5. <form-beanname="regForm"type="prj30.form.RegForm"/>
  6. </form-beans>
  7. <global-exceptions/>
  8. <global-forwards/>
  9. <action-mappings>
  10. <!--attribute="regForm"
  11. input="/reg.jsp"scope="request"-->
  12. <action
  13. name="regForm"
  14. path="/reg"
  15. type="prj30.action.RegAction"/>
  16. </action-mappings>
  17. <message-resourcesparameter="prj30.ApplicationResources"/>
  18. </struts-config>

暂时搁浅!

你可能感兴趣的:(apache,html,Hibernate,bean,struts)