`

struts2官方入门案列curd 编辑

 
阅读更多
偶然在查看文档时,看到这个demo,后来认真看了下真是麻雀虽小,很单一的struts2的增删改查,但是却从各方面诠释着struts2这一开源框架的精妙设计和丰富的可定制性。文档上提供是片段式的代码讲解,且是英文的,所以这里记录一下,方面以后查看。

和以前一样,先上效果图:

图一:

图二:

图三:

图四:

虽然从图上看的话,以上功能简单,但是代码里,时刻体现着该框架的设计之优秀,首先,我们新建一个web功能CusManager,并加入struts2的7个必要jar包。

第一步,新建两个实体类Department和Employee作POJO,代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.model;
  2. importjava.io.Serializable;
  3. publicclassDepartmentimplementsSerializable{
  4. IntegerdepartmentId;
  5. Stringname;
  6. publicDepartment(){
  7. }
  8. publicDepartment(IntegerdepartmentId,Stringname){
  9. this.departmentId=departmentId;
  10. this.name=name;
  11. }
  12. publicIntegergetDepartmentId(){
  13. returndepartmentId;
  14. }
  15. publicvoidsetDepartmentId(IntegerdepartmentId){
  16. this.departmentId=departmentId;
  17. }
  18. publicStringgetName(){
  19. returnname;
  20. }
  21. publicvoidsetName(Stringname){
  22. this.name=name;
  23. }
  24. }

Java代码
  1. packagecom.aurifa.struts2.tutorial.model;
  2. importjava.io.Serializable;
  3. publicclassEmployeeimplementsSerializable{
  4. privateIntegeremployeeId;
  5. privateIntegerage;
  6. privateStringfirstName;
  7. privateStringlastName;
  8. privateDepartmentdepartment;
  9. publicEmployee(){
  10. }
  11. publicEmployee(IntegeremployeeId,StringfirstName,StringlastName,
  12. Integerage,Departmentdepartment){
  13. this.employeeId=employeeId;
  14. this.firstName=firstName;
  15. this.lastName=lastName;
  16. this.age=age;
  17. this.department=department;
  18. }
  19. publicDepartmentgetDepartment(){
  20. returndepartment;
  21. }
  22. publicvoidsetDepartment(Departmentdepartment){
  23. this.department=department;
  24. }
  25. publicIntegergetEmployeeId(){
  26. returnemployeeId;
  27. }
  28. publicvoidsetEmployeeId(IntegeremployeeId){
  29. this.employeeId=employeeId;
  30. }
  31. publicIntegergetAge(){
  32. returnage;
  33. }
  34. publicvoidsetAge(Integerage){
  35. this.age=age;
  36. }
  37. publicStringgetFirstName(){
  38. returnfirstName;
  39. }
  40. publicvoidsetFirstName(StringfirstName){
  41. this.firstName=firstName;
  42. }
  43. publicStringgetLastName(){
  44. returnlastName;
  45. }
  46. publicvoidsetLastName(StringlastName){
  47. this.lastName=lastName;
  48. }
  49. }

第二步,定义dao层的接口,代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.dao;
  2. importjava.util.List;
  3. importjava.util.Map;
  4. publicinterfaceDepartmentDao{
  5. publicListgetAllDepartments();
  6. publicMapgetDepartmentsMap();
  7. }

Java代码
  1. packagecom.aurifa.struts2.tutorial.dao;
  2. importjava.util.List;
  3. importcom.aurifa.struts2.tutorial.model.Employee;
  4. publicinterfaceEmployeeDao{
  5. publicListgetAllEmployees();
  6. publicEmployeegetEmployee(Integerid);
  7. publicvoidupdate(Employeeemp);
  8. publicvoidinsert(Employeeemp);
  9. publicvoiddelete(Integerid);
  10. }

第三步,完成上述接口的实现类,代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.dao;
  2. importjava.util.*;
  3. importcom.aurifa.struts2.tutorial.model.Department;
  4. publicclassDepartmentNoDBdaoimplementsDepartmentDao{
  5. privatestaticListdepartments;
  6. privatestaticMapdepartmentsMap;
  7. static{
  8. departments=newArrayList();
  9. departments.add(newDepartment(newInteger(100),"Accounting"));
  10. departments.add(newDepartment(newInteger(200),"R&D"));
  11. departments.add(newDepartment(newInteger(300),"Sales"));
  12. departmentsMap=newHashMap();
  13. Iteratoriter=departments.iterator();
  14. while(iter.hasNext()){
  15. Departmentdept=(Department)iter.next();
  16. departmentsMap.put(dept.getDepartmentId(),dept);
  17. }
  18. }
  19. publicListgetAllDepartments(){
  20. returndepartments;
  21. }
  22. publicMapgetDepartmentsMap(){
  23. returndepartmentsMap;
  24. }
  25. }

Java代码
  1. packagecom.aurifa.struts2.tutorial.dao;
  2. importjava.util.ArrayList;
  3. importjava.util.Iterator;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importorg.apache.commons.logging.Log;
  7. importorg.apache.commons.logging.LogFactory;
  8. importcom.aurifa.struts2.tutorial.model.Department;
  9. importcom.aurifa.struts2.tutorial.model.Employee;
  10. publicclassEmployeeNoDBdaoimplementsEmployeeDao{
  11. privatestaticMapdepartmentsMap;
  12. privatestaticArrayListemployees;
  13. static{
  14. employees=newArrayList();
  15. employees.add(newEmployee(newInteger(1),"John","Doe",newInteger(36),newDepartment(newInteger(100),"Accounting")));
  16. employees.add(newEmployee(newInteger(2),"Bob","Smith",newInteger(25),newDepartment(newInteger(300),"Sales")));
  17. DepartmentDaodeptDao=newDepartmentNoDBdao();
  18. departmentsMap=deptDao.getDepartmentsMap();
  19. }
  20. Loglogger=LogFactory.getLog(this.getClass());
  21. publicListgetAllEmployees(){
  22. returnemployees;
  23. }
  24. publicEmployeegetEmployee(Integerid){
  25. Employeeemp=null;
  26. Iteratoriter=employees.iterator();
  27. while(iter.hasNext()){
  28. emp=(Employee)iter.next();
  29. if(emp.getEmployeeId().equals(id)){
  30. break;
  31. }
  32. }
  33. returnemp;
  34. }
  35. publicvoidupdate(Employeeemp){
  36. Integerid=emp.getEmployeeId();
  37. for(inti=0;i<employees.size();i++){
  38. EmployeetempEmp=(Employee)employees.get(i);
  39. if(tempEmp.getEmployeeId().equals(id)){
  40. emp.setDepartment((Department)departmentsMap.get(emp.getDepartment().getDepartmentId()));
  41. employees.set(i,emp);
  42. break;
  43. }
  44. }
  45. }
  46. publicvoidinsert(Employeeemp){
  47. intlastId=0;
  48. Iteratoriter=employees.iterator();
  49. while(iter.hasNext()){
  50. Employeetemp=(Employee)iter.next();
  51. if(temp.getEmployeeId().intValue()>lastId){
  52. lastId=temp.getEmployeeId().intValue();
  53. }
  54. }
  55. emp.setDepartment((Department)departmentsMap.get(emp.getDepartment().getDepartmentId()));
  56. emp.setEmployeeId(newInteger(lastId+1));
  57. employees.add(emp);
  58. }
  59. publicvoiddelete(Integerid){
  60. for(inti=0;i<employees.size();i++){
  61. EmployeetempEmp=(Employee)employees.get(i);
  62. if(tempEmp.getEmployeeId().equals(id)){
  63. employees.remove(i);
  64. break;
  65. }
  66. }
  67. }
  68. }


第四步,根据dao层,完成service层(因为代码较为简单,未明确分包),代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.service;
  2. importjava.util.List;
  3. importcom.aurifa.struts2.tutorial.dao.DepartmentDao;
  4. importcom.aurifa.struts2.tutorial.dao.DepartmentNoDBdao;
  5. publicclassDepartmentDaoServiceimplementsDepartmentService{
  6. privateDepartmentDaodao;
  7. publicDepartmentDaoService(){
  8. this.dao=newDepartmentNoDBdao();
  9. }
  10. publicListgetAllDepartments(){
  11. returndao.getAllDepartments();
  12. }
  13. }

Java代码
  1. packagecom.aurifa.struts2.tutorial.service;
  2. importjava.util.List;
  3. importcom.aurifa.struts2.tutorial.dao.EmployeeDao;
  4. importcom.aurifa.struts2.tutorial.dao.EmployeeNoDBdao;
  5. importcom.aurifa.struts2.tutorial.model.Employee;
  6. publicclassEmployeeDaoServiceimplementsEmployeeService{
  7. privateEmployeeDaodao;
  8. publicEmployeeDaoService(){
  9. this.dao=newEmployeeNoDBdao();
  10. }
  11. publicListgetAllEmployees(){
  12. returndao.getAllEmployees();
  13. }
  14. publicvoidupdateEmployee(Employeeemp){
  15. dao.update(emp);
  16. }
  17. publicvoiddeleteEmployee(Integerid){
  18. dao.delete(id);
  19. }
  20. publicEmployeegetEmployee(Integerid){
  21. returndao.getEmployee(id);
  22. }
  23. publicvoidinsertEmployee(Employeeemp){
  24. dao.insert(emp);
  25. }
  26. }

第五步,service层的接口,代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.service;
  2. importjava.util.List;
  3. publicinterfaceDepartmentService{
  4. publicListgetAllDepartments();
  5. }

Java代码
  1. packagecom.aurifa.struts2.tutorial.service;
  2. importjava.util.List;
  3. importcom.aurifa.struts2.tutorial.model.Employee;
  4. publicinterfaceEmployeeService{
  5. publicListgetAllEmployees();
  6. publicvoidupdateEmployee(Employeeemp);
  7. publicvoiddeleteEmployee(Integerid);
  8. publicEmployeegetEmployee(Integerid);
  9. publicvoidinsertEmployee(Employeeemp);
  10. }

第六步,则是action层,代码如下:

Java代码
  1. packagecom.aurifa.struts2.tutorial.action;
  2. importjava.util.List;
  3. importorg.apache.commons.logging.Log;
  4. importorg.apache.commons.logging.LogFactory;
  5. importcom.aurifa.struts2.tutorial.model.Employee;
  6. importcom.aurifa.struts2.tutorial.service.DepartmentDaoService;
  7. importcom.aurifa.struts2.tutorial.service.DepartmentService;
  8. importcom.aurifa.struts2.tutorial.service.EmployeeDaoService;
  9. importcom.aurifa.struts2.tutorial.service.EmployeeService;
  10. importcom.opensymphony.xwork2.ActionSupport;
  11. importcom.opensymphony.xwork2.Preparable;
  12. publicclassEmployeeActionextendsActionSupportimplementsPreparable{
  13. /**
  14. *
  15. */
  16. privatestaticfinallongserialVersionUID=-6886717038958304064L;
  17. privateLoglogger=LogFactory.getLog(this.getClass());
  18. privatestaticEmployeeServiceempService=newEmployeeDaoService();
  19. privatestaticDepartmentServicedeptService=newDepartmentDaoService();
  20. privateEmployeeemployee;
  21. privateListemployees;
  22. privateListdepartments;
  23. publicvoidprepare()throwsException{
  24. departments=deptService.getAllDepartments();
  25. if(employee!=null&&employee.getEmployeeId()!=null){
  26. employee=empService.getEmployee(employee.getEmployeeId());
  27. }
  28. }
  29. publicStringdoSave(){
  30. if(employee.getEmployeeId()==null){
  31. empService.insertEmployee(employee);
  32. }else{
  33. empService.updateEmployee(employee);
  34. }
  35. returnSUCCESS;
  36. }
  37. publicStringdoDelete(){
  38. empService.deleteEmployee(employee.getEmployeeId());
  39. returnSUCCESS;
  40. }
  41. publicStringdoList(){
  42. employees=empService.getAllEmployees();
  43. returnSUCCESS;
  44. }
  45. publicStringdoInput(){
  46. returnINPUT;
  47. }
  48. /**
  49. *@returnReturnstheemployee.
  50. */
  51. publicEmployeegetEmployee(){
  52. returnemployee;
  53. }
  54. /**
  55. *@paramemployee
  56. *Theemployeetoset.
  57. */
  58. publicvoidsetEmployee(Employeeemployee){
  59. this.employee=employee;
  60. }
  61. /**
  62. *@returnReturnstheemployees.
  63. */
  64. publicListgetEmployees(){
  65. returnemployees;
  66. }
  67. /**
  68. *@returnReturnsthedepartments.
  69. */
  70. publicListgetDepartments(){
  71. returndepartments;
  72. }
  73. }

接着在同级目录下,我们添加该action的同名验证框架,代码如下:

Employeeaction-crud-validation.xml代码
  1. <!DOCTYPEvalidatorsPUBLIC"-//OpenSymphonyGroup//XWorkValidator1.0.2//EN"
  2. "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
  3. <validators>
  4. <fieldname="employee.firstName">
  5. <field-validatortype="requiredstring">
  6. <messagekey="errors.required.firstname"/>
  7. </field-validator>
  8. </field>
  9. <fieldname="employee.lastName">
  10. <field-validatortype="requiredstring">
  11. <messagekey="errors.required.lastname"/>
  12. </field-validator>
  13. </field>
  14. <fieldname="employee.age">
  15. <field-validatortype="required"short-circuit="true">
  16. <messagekey="errors.required.age"/>
  17. </field-validator>
  18. <field-validatortype="int">
  19. <paramname="min">18</param>
  20. <paramname="max">65</param>
  21. <messagekey="errors.required.age.limit"/>
  22. </field-validator>
  23. </field>
  24. </validators>

至此,代码部分大抵完成,下面在src下,新建所需的配置文件:

首先是log4j,代码如下:

Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPElog4j:configurationSYSTEM"log4j.dtd">
  3. <log4j:configurationxmlns:log4j="http://jakarta.apache.org/log4j/">
  4. <appendername="stdout"class="org.apache.log4j.ConsoleAppender">
  5. <layoutclass="org.apache.log4j.TTCCLayout"/>
  6. </appender>
  7. <!--logdetailconfiguration-->
  8. <loggername="com.opensymphony.xwork">
  9. <levelvalue="error"/>
  10. <appender-refref="stdout"/>
  11. </logger>
  12. <loggername="com.opensymphony.webwork">
  13. <levelvalue="error"/>
  14. <appender-refref="stdout"/>
  15. </logger>
  16. <loggername="freemarker">
  17. <levelvalue="warn"/>
  18. <appender-refref="stdout"/>
  19. </logger>
  20. <loggername="com.mevipro">
  21. <levelvalue="debug"/>
  22. <appender-refref="stdout"/>
  23. </logger>
  24. <root>
  25. <levelvalue="error"/>
  26. <appender-refref="stdout"/>
  27. </root>
  28. </log4j:configuration>

其次是struts.xml,代码如下:

Xml代码
  1. <!DOCTYPEstrutsPUBLIC
  2. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">
  4. <struts>
  5. <!--Includewebworkdefault(fromtheStrutsJAR).-->
  6. <includefile="struts-default.xml"/>
  7. <!--Configurationforthedefaultpackage.-->
  8. <packagename="default"extends="struts-default">
  9. <!--Defaultinterceptorstack.-->
  10. <default-interceptor-refname="paramsPrepareParamsStack"/>
  11. <actionname="index"class="com.aurifa.struts2.tutorial.action.EmployeeAction"method="list">
  12. <resultname="success">/jsp/employees.jsp</result>
  13. <!--wedon'tneedthefullstackhere-->
  14. <interceptor-refname="basicStack"/>
  15. </action>
  16. <actionname="crud"class="com.aurifa.struts2.tutorial.action.EmployeeAction"method="input">
  17. <resultname="success"type="redirect-action">index</result>
  18. <resultname="input">/jsp/employeeForm.jsp</result>
  19. <resultname="error">/jsp/error.jsp</result>
  20. </action>
  21. </package>
  22. </struts>

然后是struts的国际化文件,struts.properties和guest.properties,代码分别如下:

Struts.properties代码
  1. struts.custom.i18n.resources=guest

Guest.properties代码
  1. #labels
  2. application.title=EmployeeMaintenanceApplication
  3. label.employees=Employees
  4. label.delete=Delete
  5. label.edit=Edit
  6. label.employee.edit=EditEmployee
  7. label.employee.add=AddEmployee
  8. label.firstName=FirstName
  9. label.lastName=LastName
  10. label.department=Department
  11. label.age=Age
  12. #buttonlabels
  13. button.label.submit=Submit
  14. button.label.cancel=Cancel
  15. ##--errors
  16. errors.prefix=<spanstyle="color:red;font-weight:bold;">
  17. errors.suffix=</span>
  18. errors.general=AnErrorHasOcccured
  19. errors.required.firstname=Nameisrequired.
  20. errors.required.lastname=Lastnameisrequired.
  21. errors.required.age=Pleaseprovideanage.
  22. errors.required.age.limit=Pleaseprovideanagebetween${min}and${max}.
  23. errors.required.department=Departmentisrequired.

最后贴上运行时的jsp页面,代码不多,下面我分别贴出来,代码如下:

Index.jsp代码
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%
  3. Stringpath=request.getContextPath();
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  7. <html>
  8. <head>
  9. <basehref="<%=basePath%>">
  10. <title>MyJSP'index.jsp'startingpage</title>
  11. <metahttp-equiv="pragma"content="no-cache">
  12. <metahttp-equiv="cache-control"content="no-cache">
  13. <metahttp-equiv="expires"content="0">
  14. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  15. <metahttp-equiv="Refresh"content="0;URL=index.action">
  16. <!--
  17. <linkrel="stylesheet"type="text/css"href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. ThisismyJSPpage.<br>
  22. </body>
  23. </html>

Employeeform.jsp代码
  1. <%@pagecontentType="text/html;charset=UTF-8"language="java"%>
  2. <%@taglibprefix="s"uri="/struts-tags"%>
  3. <s:iftest="employee==null||employee.employeeId==null">
  4. <s:setname="title"value="%{'Addnewemployee'}"/>
  5. </s:if>
  6. <s:else>
  7. <s:setname="title"value="%{'Updateemployee'}"/>
  8. </s:else>
  9. <html>
  10. <head>
  11. <linkhref="css/main.css"rel="stylesheet"type="text/css"/>
  12. <style>td{white-space:nowrap;}</style>
  13. <title><s:propertyvalue="#title"/></title>
  14. </head>
  15. <body>
  16. <divclass="titleDiv"><s:textname="application.title"/></div>
  17. <h1><s:propertyvalue="#title"/></h1>
  18. <%--<s:actionerror/>--%>
  19. <%--<s:actionmessage/>--%>
  20. <s:formaction="crud!save.action"method="post">
  21. <s:textfieldname="employee.firstName"value="%{employee.firstName}"label="%{getText('label.firstName')}"size="40"/>
  22. <s:textfieldname="employee.lastName"value="%{employee.lastName}"label="%{getText('label.lastName')}"size="40"/>
  23. <s:textfieldname="employee.age"value="%{employee.age}"label="%{getText('label.age')}"size="20"/>
  24. <s:selectname="employee.department.departmentId"value="%{employee.department.departmentId}"list="departments"listKey="departmentId"listValue="name"/>
  25. <%--<s:selectname="gender"list="%{#{'male':'Male','female':'Female'}}"/>--%>
  26. <s:hiddenname="employee.employeeId"value="%{employee.employeeId}"/>
  27. <s:submitvalue="%{getText('button.label.submit')}"/>
  28. <s:submitvalue="%{getText('button.label.cancel')}"name="redirect-action:index"/>
  29. </s:form>
  30. </body>
  31. </html>

Employees.jsp代码
  1. <%@pagecontentType="text/html;charset=UTF-8"language="java"%>
  2. <%@taglibprefix="s"uri="/struts-tags"%>
  3. <html>
  4. <head>
  5. <linkhref="css/main.css"rel="stylesheet"type="text/css"/>
  6. <title><s:textname="label.employees"/></title>
  7. </head>
  8. <body>
  9. <divclass="titleDiv"><s:textname="application.title"/></div>
  10. <h1><s:textname="label.employees"/></h1>
  11. <s:urlid="url"action="crud!input"/>
  12. <ahref="<s:propertyvalue="#url"/>">AddNewEmployee</a>
  13. <br/><br/>
  14. <tableclass="borderAll">
  15. <tr>
  16. <th><s:textname="label.firstName"/></th>
  17. <th><s:textname="label.lastName"/></th>
  18. <th><s:textname="label.age"/></th>
  19. <th><s:textname="label.department"/></th>
  20. <th>&nbsp;</th>
  21. </tr>
  22. <s:iteratorvalue="employees"status="status">
  23. <trclass="<s:iftest="#status.even">even</s:if><s:else>odd</s:else>">
  24. <tdclass="nowrap"><s:propertyvalue="firstName"/></td>
  25. <tdclass="nowrap"><s:propertyvalue="lastName"/></td>
  26. <tdclass="nowrap"><s:propertyvalue="age"/></td>
  27. <tdclass="nowrap"><s:propertyvalue="department.name"/></td>
  28. <tdclass="nowrap">
  29. <s:urlaction="crud!input"id="url">
  30. <s:paramname="employee.employeeId"value="employeeId"/>
  31. </s:url>
  32. <ahref="<s:propertyvalue="#url"/>">Edit</a>
  33. &nbsp;&nbsp;&nbsp;
  34. <s:urlaction="crud!delete"id="url">
  35. <s:paramname="employee.employeeId"value="employeeId"/>
  36. </s:url>
  37. <ahref="<s:propertyvalue="#url"/>">Delete</a>
  38. </td>
  39. </tr>
  40. </s:iterator>
  41. </table>
  42. </body>
  43. </html>

Error.jsp代码
  1. <%@pagecontentType="text/html;charset=UTF-8"language="java"%>
  2. <%@taglibprefix="s"uri="/struts-tags"%>
  3. <html>
  4. <head>
  5. <title>ErrorPage</title>
  6. <linkhref="<s:urlvalue='/css/main.css'/>"rel="stylesheet"type="text/css"/>
  7. </head>
  8. <body>
  9. <s:actionerror/>
  10. <br/>
  11. Inorderthatthedevelopmentteamcanaddressthiserror,pleasereportwhatyouweredoingthatcausedthiserror.
  12. <br/><br/>
  13. Thefollowinginformationcanhelpthedevelopment
  14. teamfindwheretheerrorhappenedandwhatcanbedonetopreventitfrom
  15. happeninginthefuture.
  16. </body>
  17. </html>

以上页面引用到的css文件,代码如下:

Main.css代码
  1. html,body{
  2. margin-left:10px;
  3. margin-right:10px;
  4. margin-bottom:5px;
  5. color:black;
  6. background-color:white;
  7. font-family:Verdana,Arial,sans-serif;
  8. font-size:12px;
  9. }
  10. .titleDiv{
  11. background-color:#EFFBEF;
  12. font-weight:bold;
  13. font-size:18px;
  14. text-align:left;
  15. padding-left:10px;
  16. padding-top:10px;
  17. padding-bottom:10px;
  18. border:2pxsolid#8F99EF;
  19. }
  20. h1{font-weight:bold;color:brown;font-size:15px;text-align:left;}
  21. td{font-size:12px;padding-right:10px;}
  22. th{text-align:left;font-weight:bold;font-size:13px;padding-right:10px;}
  23. .tdLabel{font-weight:bold;white-space:nowrap;vertical-align:top;}
  24. A{color:#4A825A;text-decoration:none;}
  25. A:link{text-decoration:none;}
  26. A:visited{text-decoration:none;}
  27. A:hover{text-decoration:none;color:red;}
  28. .borderAll{
  29. border:2pxsolid#8F99EF;
  30. }
  31. .butStnd{
  32. font-family:arial,sans-serif;
  33. font-size:11px;
  34. width:105px;
  35. background-color:#DCDFFA;color:#4A825A;font-weight:bold;
  36. }
  37. .error{
  38. color:red;
  39. font-weight:bold;
  40. }
  41. .errorSection{
  42. padding-left:18px;
  43. padding-top:2px;
  44. padding-bottom:10px;
  45. padding-right:5px;
  46. }
  47. .even{background-color:#EFFBEF;}
  48. .odd{background-color:white;}
  49. .nowrap{white-space:nowrap;}

到这里,这个例子基本上就完成了,其主要亮点在action层的实现类和验证xml,而jsp页面的tag和struts.xml也比较耐看,将web.xml配置好struts2的监听以后,部署到tomcat之后,浏览器键入:localhost:8080/CusManager,即可得到以上效果图所示。

代码我是全部都贴上了,连css也有,不会有任何地方的缺失,假使大家运行报错,请认真查看错误,并检查自己的web.xml的配置,除此之外不会有任何错误。

分享到:
评论

相关推荐

    struts2官方入门案列curd

    这个官方入门案例“struts2官方入门案列curd”着重于介绍如何使用Struts2实现CRUD(创建、读取、更新、删除)操作,这是任何Web应用中最基本的功能需求。下面我们将详细探讨Struts2框架以及如何通过它实现CRUD操作。...

    Struts2-Sqlite3-CURD

    总的来说,"Struts2-Sqlite3-CURD"项目展示了如何利用Struts2框架的灵活性和SQLite3的便捷性,构建一个功能完备的Web应用程序,用于管理和操作数据库。这对于学习Java web开发和SQLite数据库操作的初学者是一个很好...

    Struts2+Hibernate+Spring整合与增、删、改、查CURD操作

    Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自负责应用程序的不同层面:Struts2处理MVC(Model-View-Controller)架构中的控制层,Hibernate专注于数据持久化,而Spring则提供了全面的依赖注入...

    Struts2+Spring+IBatis实现CURD

    在Java Web开发中,"Struts2+Spring+IBatis"是一个常见的企业级应用框架组合,也被称为SSI(Struts2、Spring、iBatis)框架。这种组合提供了模型-视图-控制器(MVC)架构,事务管理,以及灵活的数据访问机制。以下是...

    struts2与hibernate的整合实现数据的crud

    Struts2和Hibernate是两种非常流行的Java开源框架,它们分别用于MVC(Model-View-Controller)架构的控制层和持久层的管理。Struts2提供了强大的动作调度和页面渲染能力,而Hibernate则是面向对象的数据库映射工具,...

    struts2+hibernate整合 练习之CURD 完整版

    Struts2和Hibernate是两种非常重要的Java开源框架,它们在Web开发中被广泛使用。Struts2是一个MVC(Model-View-Controller)框架,它主要用于处理用户请求和控制应用程序流程,而Hibernate则是一个对象关系映射(ORM...

    struts2DeptCURD

    "struts2DeptCURD"这个项目很可能是关于在Struts2框架下实现部门管理的CRUD(Create, Read, Update, Delete)操作。下面我们将深入探讨Struts2框架及其在CRUD操作中的应用。 首先,Struts2作为MVC框架的核心组件,...

    struts2 CRUD

    Struts2+Mysql实现CURD,stuts2中使用servlet中Request,session,context对象

    Struts2+Hibernate实现新闻发布系统

    Struts2和Hibernate是两种非常重要的Java Web开发框架,它们在构建新闻发布系统中起到了关键作用。Struts2作为MVC(Model-View-Controller)架构的一部分,主要负责控制应用程序的流程,而Hibernate则是一个对象关系...

    Struts、Hibernate、Spring实现CURD所用Jar包(Lib1)

    在这个名为"Struts、Hibernate、Spring实现CURD所用Jar包(Lib1)"的压缩包中,包含的是用于实现数据库CRUD操作(创建Create、读取Read、更新Update、删除Delete)所需的基础库文件。 Struts是Apache组织开发的一个...

    javaWeb_struts2框架实现简单用户注册登录

    2.数据库操作使用c3p0连接池和dbtuils组件,对表的CURD,二者搭配感觉很easy,没有使用hibernate。 3.控制器采用action开发,替代传统的servlet,直接跳转页面返回一个字符串即可,需配置struts.xml对应的jsp。 4....

    struts2+spring+ibaites+oracle+CURD

    Struts2、Spring、iBatis 和 Oracle 是四个...综上所述,"struts2+spring+ibaites+oracle+CURD" 这个组合提供了完整的 Java Web 应用解决方案,涵盖了从用户交互到数据库操作的整个流程,是企业级开发中常见的技术栈。

    struts2curd

    Struts2 CRUD是一个基于Apache Struts 2框架的通用数据操作模块,主要用于简化Web应用中的创建、读取、更新和删除(CRUD)操作。这个模块可以帮助开发者快速构建数据管理功能,减少重复代码,提高开发效率。Struts2...

    Struts 2.1.8_学习源码

    Struts2_02CURD : 关于Struts2的增、删、改和查 实际业务中数据来自数据库,从DAO层查询,本实例使用静态资源的方式模拟, 主要是关于CURD的操作方式。 Struts2_03Taglib : Struts2常用标签的使用方法 Struts2...

    Struts2+Oracle增删查

    这个压缩包文件“Struts2.CURD”很可能包含了实现上述功能的源代码,包括Action类、DAO接口及实现、配置文件和视图文件。开发者可以通过查看这些文件,学习如何结合Struts2和Oracle实现基本的数据操作功能。

    jfinal入门CURD

    JFinal入门CURD实现对数据库基本的增删改查,EclipseEE+Mysql+Tomcat7 在主页面可以增加用户,删除用户,修改用户. 1.解压后将jfinallearn1.sql的数据弄到自己的mysql里(只有一张user表) 2.修改jfinalLearn1\src...

    Spring Boot 整合 SSM 下 CURD 示例

    在本示例中,我们将深入探讨如何在Spring Boot框架中整合Spring、Struts2和MyBatis(SSM)来实现完整的数据CURD操作。Spring Boot以其简洁的配置和快速的开发能力,使得构建Web应用变得更加高效。接下来,我们将详细...

Global site tag (gtag.js) - Google Analytics