1. 程式人生 > >spring hibernate struts整合開發例項

spring hibernate struts整合開發例項

">

<struts-config>
<data-sources />
<form-beans >
  <form-bean name="membersForm" type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="sex" type="java.lang.String" />
    <form-property name="password" type="java.lang.String" />
    <form-property name="mail" type="java.lang.String" />
    <form-property name="name" type="java.lang.String" />
  </form-bean>

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
  <action
    attribute="membersForm"
    name="membersForm"
    parameter="action"
    path="/members"
    scope="request"
    type="org.spring
framework.web.struts.DelegatingActionProxy" >
    <forward name="loginSuccess" path="/index.jsp" />
    <forward name="registerSuccess" path="/index.jsp" />
    <forward name="logoutSuccess" path="/index.jsp" />
  </action>


</action-mappings>

<message-resources parameter="com.yang.product.booksonline.ApplicationResources" />
<plug-in className="org.apache.struts
.validator.ValidatorPlugIn">
  <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  <set-property property="stopOnFirstError" value="true" />
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/spring
Context.xml" />
</plug-in>

</struts-config>

package com.yang.product.booksonline.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.yang.product.booksonline.dao.Members;
import com.yang.product.booksonline.dao.MembersDAO;

/** 
* XDoclet definition:
* @struts.action path="/members" name="membersForm" parameter="action" scope="request" validate="true"
*/
public class MembersAction extends DispatchAction {
     private MembersDAO membersDAO;

     public MembersDAO getMembersDAO() {
           return membersDAO;
     }

     public void setMembersDAO(MembersDAO membersDAO) {
           this.membersDAO = membersDAO;
     }
     
     public ActionForward login (ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
//            String url = request.getParameter("url");
           String name = (String)((DynaActionForm)form).get("name");
           String password = (String)((DynaActionForm)form).get("password");
           List members = membersDAO.findByMembersName(name);
           if (members != null && members.size() >0) {
                 Members member = (Members)members.get(0);
                 if (password.equals(member.getPassword())) {
                       request.getSession().setAttribute("member",name);
                       //return new ActionForward(url);
                       return mapping.findForward("loginSuccess");
                 } else {
                       return null;
                 }
           } else {
                 return null;
           }
     }

     public ActionForward logout (ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
           request.getSession().removeAttribute("member");
           return mapping.findForward("logoutSuccess");
     }

     public ActionForward register (ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
           String name = (String)((DynaActionForm)form).get("name");
           String password = (String)((DynaActionForm)form).get("password");
           String sex = (String)((DynaActionForm)form).get("sex");
           String mail = (String)((DynaActionForm)form).get("mail");
           List members = membersDAO.findByMembersName(name);
           if (members != null && members.size() >0) {
                 return null;
           } else {
                 Members member = new Members();
                 member.setName(name);
                 member.setPassword(password);
                 member.setSex(sex);
                 member.setMail(mail);
                 membersDAO.save(member);
                 return mapping.findForward("registerSuccess");
           }
     }
}

注意到,這裡使用了dispatchaction。
五,三個jsp部分內容
index.jsp
<body>
<logic:notPresent name="member" scope="session">
     Welcome,you have not <html:link href="login.jsp">login</html:link>. <br>
</logic:notPresent>
<logic:present name="member" scope="session">
     Welcome,<bean:write name="member" scope="session"/>.<html:link href="members.do?action=logout">Logout</html:link>
</logic:present>
</body>
login.jsp
<body>
  If you haven't registered the system,please <html:link href="register.jsp">register</html:link>
  <html:form action="/members.do?action=login" method="post" focus="name">
    <table border="0">
    <tr>
      <td><bean:message key="members.form.name.label"/>:</td>
      <td><html:text property="name" /></td>
    </tr>
    <tr>
      <td><bean:message key="members.form.password.label"/>:</td>
      <td><html:password property="password" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><html:submit><bean:message key="members.form.login.label"/></html:submit></td>
    </tr>
    </table>
  </html:form>
</body>
register.jsp
<body>
  Welcome to register the books online. <br>
  <html:form action="/members.do?action=register" method="POST">
  <bean:message key="members.form.name.label"/>:<html:text property="name"/><br>
  <bean:message key="members.form.password.label"/>:<html:password property="password"/><br>
  <bean:message key="members.form.sex.label"/>:
  <html:select property="sex">
       <html:option key="members.form.sex.male" value="male" />
       <html:option key="members.form.sex.female" value="female" />
  </html:select><br>
  <bean:message key="members.form.mail.label"/>:<html:text property="mail"/><br>
  <html:submit><bean:message key="members.form.register.label"/></html:submit>
  </html:form>
</body>

注意:在開發過程中,本想加上輸入驗證的,因為前面己經有了這方面的例子,為了不重複,這裡就有意去掉了這部分,但要執行這個例子,還是你自己加上validation.xml、validator-rules.xml兩個檔案,不然會出錯。