registeraction.java

来自「java开发者突击源代码demo(struts).rar」· Java 代码 · 共 60 行

JAVA
60
字号
package com.demo.struts.actions;

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

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

import com.demo.struts.forms.RegisterForm;
import com.demo.struts.util.Constants;

public class RegisterAction extends DispatchAction {
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = mapping.findForward(Constants.FAILURE_KEY);
		return (forward);
	}

	public ActionForward register(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();
		RegisterForm loginForm = (RegisterForm) form;

		try {
			String username = loginForm.getUsername();
			String password1 = loginForm.getPassword1();
			String password2 = loginForm.getPassword2();
			String email = loginForm.getEmail();
			
			boolean isRegister = false;
			if(username.equals("admin")){
				isRegister = true;
			}
			if(!isRegister){
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
				"register.error.failed"));
			}
		} catch (Exception e) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
					"register.error.failed"));
		}

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward(Constants.FAILURE_KEY);
		} else {
			forward = mapping.findForward(Constants.SUCCESS_KEY);
		}
		return (forward);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?