some coments of struts 1

1 validation of the form

 1.1 declare to validate the form through formbean

 1  <!-- *******************************Login **************************** -->
 2         <action
 3             path="/login"
 4             type="com.component.asm.struts.LoginAction"
 5             name="loginForm"   <!-- the form to be validated -->
 6             validate  = "true"   <!-- set as true -->
 7             input = "/index.jsp"  <!-- if validation not passed, forward to this page, redirect=false -->
 8             scope="request">
 9             
10            <forward name="loginSuccess" path="/pages/home.jsp" /> <!--redirect=false,you can set as true or forward another action directly instead. -->
11            <forward name="loginFail" path="/index.jsp" />
12          </action>

 1.2 Overwrite the method validate() in the loginForm

  1 @Override

 2     public ActionErrors validate(ActionMapping mapping,
 3             HttpServletRequest request) {
 4         ActionErrors errors = new ActionErrors();
 5         if (!("me".equals(username) || "LiRongJian".equals(username) || "GongXiaoQi".equals(username)))
 6         {  

 7            errors.add("username"new ActionMessage("error.username"));  //here will generate an ActionMessage
 8        }
 9return errors;
10    }

 1.3 define the message for "error.username" in MessageResources.properties

1 -- validator --
2 error.username=Please input a valid username.

 1.4 show the error message in the login page:

1 <font color="#FF0000"><html:errors property="username"/></font>

So far, the setting is ready.  When you input an invalid username, red error message will be show in the login page.

 2 enquiry according to entity

 1 public List<Customer> search(Customer customer) {
 2         List<Customer> list = new ArrayList<Customer>();
 3         Session session = Utils.getSession();
 4         session.beginTransaction();
 5         Criteria criteria = session.createCriteria(Customer.class);
 6         criteria.add(Restrictions.eq("customer_id", customer.getCustomer_id()));
 7         criteria.addOrder(Order.desc("customer_id"));
 8         list = criteria.list();
 9         session.getTransaction().commit();
10         Utils.closeSession(session);

 3 type of the fields in form-bean <--> type of the column of the database, especially for Date type

4  one-to-one relationship in  hibernate 

5 Exception: "a different object with the same identifier value was already associated with the session"

comment: the current session manager two entity with the same name, the recommended way is to close the session before manager another entity.

6 absolute path

String path = request.getContextPath(); //the path of my application

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"

7 go back button

href="java:history.back(-1)" 

8 we can create a "dynamic web project " using MyEclipse instead of create a java project using eclipse

原文地址:https://www.cnblogs.com/kelin1314/p/1870958.html