loginbean.java

来自「jsf+spring+ibatis 的一个程序,主要用来介绍JSF程序.」· Java 代码 · 共 72 行

JAVA
72
字号
package com.wxd.common.popedom.presentation;

import java.io.IOException;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wxd.common.popedom.service.LogService;
import com.wxd.common.popedom.service.ServiceFactory;
import com.wxd.util.MessageFactory;

public class LoginBean {
	private HtmlInputText userid;
	private HtmlInputSecret password;

	public HtmlInputText getUserid() {
		return userid;
	}

	public void setUserid(HtmlInputText userid) {
		this.userid = userid;
	}

	public HtmlInputSecret getPassword() {
		return password;
	}

	public void setPassword(HtmlInputSecret password) {
		this.password = password;
	}

	public String login() {
		LogService logService = ServiceFactory.getLogService(FacesContext.getCurrentInstance());
		boolean result = logService.isValidUser(this.getUserid().getValue()
				.toString().trim(), this.getPassword().getValue().toString()
				.trim());
		if (result)
			return "success";
		else {
			FacesMessage msg = MessageFactory.getMessage(FacesContext.getCurrentInstance(),
					"error_password", "");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return "failure";
		}
	}

	public String logout() throws IOException {
		ExternalContext ectx = FacesContext.getCurrentInstance()
				.getExternalContext();
		HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
		//        HttpSession session = (HttpSession)ectx.getSession(false);
		//        session.invalidate();    

		//        FacesContext ctx =  FacesContext.getCurrentInstance();
		//        Application app = ctx.getApplication();
		//        app.getNavigationHandler().handleNavigation(ctx, "/index.jsp", "main");

		// To avoid using the navigation handler you could also use...
		response.sendRedirect("../index.jsp");
		FacesContext.getCurrentInstance().responseComplete(); //jsf1.2不能返回null,必须使用此方法重定向 
		return null;

	}
}

⌨️ 快捷键说明

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