SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(6)

 

i)            编写UserAction.java

UserAction,主要通过两个方法login,register,通过调用UserService.java里面对应的方法,实现了两个逻辑 :登陆和用户注册.

另外有两个方法 :toLogintoRegister,分别只是转向到登陆和注册的初始化页面.

另外有定义了一个成员变量private UserService userService 和对应的符合JavaBean规范的set方法.它们的作用,同我们编写的UserService.java类里面,定义的userDAO属性和setUserDAO方法一样.

还有,代码中有根据调用userService方法,返回的值的不同,message赋予不同的值,那值是操作结果的信息提示,具体定义是在UserResouce_temp.properties里面的标签,是给页面操作结果做提示用的.

UserAction.java的具体代码如下 :

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package cn.com.book.demo.struts.action;

 

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.actions.DispatchAction;

 

import cn.com.book.demo.services.UserServices;

import cn.com.book.demo.services.impl.UserServicesImpl;

import cn.com.book.demo.struts.form.LoginForm;

import cn.com.book.demo.struts.form.RegisterForm;

 

/**

 * MyEclipse Struts Creation date: 12-08-2007

 *

 * XDoclet definition:

 *

 * @struts.action path="/login" name="loginForm"

 *                input="/WEB-INF/jsp/user/login.jsp" parameter="method"

 *                scope="request" validate="true"

 * @struts.action-forward name="success" path="/WEB-INF/jsp/user/login.jsp"

 * @struts.action-forward name="error" path="/WEB-INF/jsp/user/login.jsp"

 * @struts.action-forward name="register" path="/WEB-INF/jsp/user/register.jsp"

 */

public class UserAction extends DispatchAction {

      private UserServices userService = null;

 

      /*

       * Generated Methods

       */

 

      public void setUserService(UserServices userService) {

             this.userService = userService;

      }

 

      /**

       * Method login

       *

       * @param mapping

       * @param form

       * @param request

       * @param response

       * @return ActionForward

       */

      public ActionForward login(ActionMapping mapping, ActionForm form,

                    HttpServletRequest request, HttpServletResponse response) {

             String path = "error";

             String message = "user.verify.success";

             this.initServices();

             LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method

                                                                                   // stub

 

             String userName = loginForm.getUserName();

             String password = loginForm.getPassword();

             int result = this.userService.verifyUser(userName, password);

 

             if (result == 0) {

                    path = "success";

             } else {

                    if (result == 1) { // 用户名不存在

                           message = "user.no.username";

                    } else if (result == 2) {// 用户名密码错误

                           message = "user.error.password";

                    } else { // 用户名或密码为空

                           message = "user.username.password.notnull";

                    }

             }

             loginForm.setMessage(message);

             return mapping.findForward(path);

      }

 

      public ActionForward toRegister(ActionMapping mapping, ActionForm form,

                    HttpServletRequest request, HttpServletResponse response) {

             return mapping.findForward("register");

      }

     

      public ActionForward toLogin(ActionMapping mapping, ActionForm form,

                    HttpServletRequest request, HttpServletResponse response) {

             return mapping.findForward("login");

      }

 

      public ActionForward register(ActionMapping mapping, ActionForm form,

                    HttpServletRequest request, HttpServletResponse response) {

             this.initServices();

             String path = "error";

             String message = "user.register.success";

             RegisterForm registerForm = (RegisterForm) form;// TODO Auto-generated

                                                                                                 // method stub

             String userName = registerForm.getUserName();

             String password = registerForm.getPassword();

             String name = registerForm.getName();

             String email = registerForm.getEmail();

             String phone = registerForm.getPhone();

 

             int result = this.userService.register(userName, password, name, email,

                           phone);

             if (result == 0) {

                    path = "success";

             } else {

                    if (result == 1) {// 用户名已经存在

                           message = "user.exist.username";

                    } else if (result == 2) {// 用户名为空

                           message = "user.username.isnull";

                    } else if (result == 3) {// 密码为空

                           message = "user.password.isnull";

                    } else if (result == 4) {// 姓名为空

                           message = "user.name.isnull";

                    } else {// 其它错误

                           message = "user.error.others";

                    }

             }

             registerForm.setMessage(message);

             return mapping.findForward(path);

      }

 

      private void initServices() {

             if (this.userService == null) {

                    this.userService = new UserServicesImpl();

             }

      }

}

ii)            

2.       struts-config.xml文件中,配置formaction

struts-config.xml里面,配置两个form :loginFormregisterForm

配置两个action :loginregister

以及操作的具体流程,具体配置脚本如下 :

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

 

<struts-config>

  <data-sources />

  <form-beans >

    <form-bean name="loginForm" type="cn.com.book.demo.struts.form.LoginForm" />

    <form-bean name="registerForm" type="cn.com.book.demo.struts.form.RegisterForm" />

 

  </form-beans>

  <global-exceptions />

  <global-forwards />

  <action-mappings >

    <action

      attribute="loginForm"

      input="/WEB-INF/jsp/user/login.jsp"

      name="loginForm"

      parameter="method"

      path="/login"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy">

      <forward name="login" path="/WEB-INF/jsp/user/login.jsp" />

      <forward name="success" path="/WEB-INF/jsp/user/login.jsp" />

      <forward name="error" path="/WEB-INF/jsp/user/login.jsp" />

      <forward name="register" path="/WEB-INF/jsp/user/register.do?method=toRegister" />

    </action>

    <action

      attribute="registerForm"

      input="/WEB-INF/jsp/user/register.jsp"

      name="registerForm"

      parameter="method"

      path="/register"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy">

      <forward name="success" path="/WEB-INF/jsp/user/register.jsp" />

      <forward name="error" path="/WEB-INF/jsp/user/register.jsp" />

      <forward name="register" path="/WEB-INF/jsp/user/register.jsp" />

    </action>

 

  </action-mappings>

 

  <message-resources parameter="cn.com.book.demo.struts.ApplicationResources" />

  <message-resources parameter="cn.com.book.demo.struts.UserResource" key="user"/>

  <message-resources parameter="cn.com.book.demo.struts.ShopResource" key="shop"/>

  <message-resources parameter="cn.com.book.demo.struts.OrderResource" key="order"/>

</struts-config>

 

Struts-config.xml的详细配置解释,请查看附录或Struts相关的书籍

 

细心的你应该发现一个问题,在所有action,type属性的值都是一样的,而且不是我们对应的Action,却是org.springframework.web.struts.DelegatingActionProxy.原因是我们在这里统一定义的是spring中的action代理,要使代码能正常运行起来,还需要在springbean注册文件中添加相关的配置.具体配置如下 :

applicationContext-action.xml文件中,添加如下内容 :

<bean name="/register" class="cn.com.book.demo.struts.action.UserAction">

    <property name="userService">

       <ref bean="userServiceProxy"/>

    </property>

</bean>

<bean name="/login" class="cn.com.book.demo.struts.action.UserAction">

    <property name="userService">

       <ref bean="userServiceProxy"/>

    </property>

</bean>

这两个Bean的定义,是对应的struts-config.xml中两个action类的定义.bean的名字必须同struts-config.xml中对应actionpath一样,而且以’/’开始.class是对应的action.

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值