欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsf spring

程序员文章站 2022-07-15 16:43:05
...

login.java

public class Login {

	private String name;

	private String password;

	private LoginService loginService;

	private String errMessage;

	private List<User> lstUser;

	private List<SelectItem> lstDepartments;

	private HtmlInputSecret inputPassword;

	private HtmlDataTable dataTable;
	
	private HtmlSelectOneMenu selectOneMenu;

	public Login() {
		name = null;
		password = null;
		errMessage = null;
		lstUser = null;
	}

	public String login() {
		lstUser = null;
		List<User> list = new ArrayList<User>();
		list = loginService.login(name, password);
		if (list.size() != 0) {
			errMessage = null;
			return "success";
		} else {
			errMessage = "wrong name or password";
			return "failure";
		}

	}

	// * AJAX*//
	public void view() {
		lstDepartments = new ArrayList<SelectItem>();
		lstUser = loginService.getAllAssociates();
		// get all departments

		for (String str : new String[] { "GE", "TEST", "INTEL", "MOTO" }) {
			lstDepartments.add(new SelectItem(str, str));
		}

	}

	// *ActionListener*//
	public void changePassword(ActionEvent e) {
		inputPassword.processValidators(FacesContext.getCurrentInstance());
		inputPassword.processUpdates(FacesContext.getCurrentInstance());
		loginService.changePassword(name, password);
	}

	// *validation*//
	public void validate(FacesContext context, UIComponent component, Object obj) {
		String str = (String) obj;
		if (str.length() < 2 || str.length() > 25) {
			FacesMessage message = new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "",
					"characters' lengh must greater than 1 and less than 25");
			throw new ValidatorException(message);

		}
	}

	// *valueChangeListener*//
	public void selectValueChange(ValueChangeEvent e) {
//		User user = (User) dataTable.get;
//		user.getAddress();
		HtmlSelectOneMenu selectOneMenu = (HtmlSelectOneMenu) dataTable
				.getChildren().get(3).getChildren().get(0);
		loginService.changeDepartment((String) selectOneMenu.getAttributes()
				.get("value"), dataTable.getRowIndex() + 1);
	}

	// *file upload*//
	public void uploadListener(UploadEvent event) {
		try {
			UploadItem item = event.getUploadItem();
			String fileName = item.getFileName();
			File file = new File(
					"D:\\Project\\newWorkspace\\jsf\\WebContent\\UploadFile\\"
							+ fileName.substring(fileName.lastIndexOf("\\") + 1));
			FileInputStream fis = new FileInputStream(item.getFile());
			FileOutputStream out = new FileOutputStream(file);
			int bytes = 0;
			byte[] bteFile = new byte[1024];
			while ((bytes = fis.read(bteFile)) != -1) {
				out.write(bteFile, 0, bytes);
			}
			System.out.println("文件上传成功!" + file.toString());
		} catch (Exception e) {
			System.out.println("文件上传失败!");

		}
	}
}

 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<f:view>
	<h:form id="test">
		<h:panelGroup layout="block"
			style="text-align:center;padding-top:100px;width:100%">
			<h:panelGroup layout="block" style="padding-left:165px;width:400px">
				<h:outputText value="name:"
					style="font-size: 20px;font-family: Arial;padding-right:35px"></h:outputText>
				<span><h:inputText id="txtName" value="#{loginbean.name}"
					required="true" validator="#{loginbean.validate}">
				</h:inputText></span>
				<span><h:message for="txtName" style="float:right;color:red"></h:message></span>
			</h:panelGroup>
			<br>
			<h:panelGroup layout="block" style="padding-left:165px;width:400px">
				<h:outputText value="password:"
					style="font-size: 20px;font-family: Arial;"></h:outputText>
				<span><h:inputSecret id="txtPassword"
					value="#{loginbean.password}" style="width:150px;" required="true">
					<f:validateLength maximum="25" minimum="2"></f:validateLength>
					</h:inputSecret></span>
				<span><h:message for="txtPassword"
					style="float:right;color:red"showDetail="false" showSummary="true"></h:message></span>
			</h:panelGroup>
			<h:panelGroup layout="block" style="padding-left:165px;width:400px">
				<h:outputText value="#{loginbean.errMessage}" style="color:red"
					rendered="#{loginbean.errMessage != null}" />
				<br>
				<br>
				<h:commandButton value="Login " action="#{loginbean.login}"
					style="border: 1px solid #ccc;"></h:commandButton>
			</h:panelGroup>
		</h:panelGroup>
	</h:form>
</f:view>
</body>
</html>

 

welcom.jsf

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>VIEW</title>
<style type="text/css">
.table_head_css {
	text-align: center;
	padding: 5px 25px 5px 25px;
	color: #333;
	font-family: arial;
	font-weight: bold;
	font-size: 12px;
	border-top: 1px #ccc solid;
	border-bottom: 1px #ccc solid;
	background-color: #d2e8ff;
}

.table_list {
	border: 1px solid #ccc;
	border-collapse: collapse;
	text-align: center;
	width: 50%;
	font-family: Arial;
	font-size: 12px;
}

.table_column {
	border: 1px solid #ccc;
}
</style>
<script type="text/javascript">
	function display() {
		var cbOne = document.getElementById('welcome:cb1');
		if (cbOne.style.display == '') {
			cbOne.style.display = 'none';
			document.getElementById('welcome:cb2').style.display = '';
			document.getElementById('welcome:password').style.display = '';
		} else {
			cbOne.style.display = '';
			document.getElementById('welcome:cb2').style.display = 'none';
			document.getElementById('welcome:password').style.display = 'none';
			document.getElementById('welcome:password').value = "";
		}
	}
</script>
</head>
<body>
<f:view>
	<h:form id="welcome">
		<h:panelGroup layout="block"
			style="text-align:center;padding-top:100px">
			<f:verbatim><h:outputText value="#{msg.WELCOME}"/></f:verbatim>
			<h:outputText value="#{loginbean.name}"
				style="font-size: 20px;font-family: Arial;font-weight: bolder;" />
			<br>
			<br>
		
			<h:commandButton id="cb1" style="border: 1px solid #ccc;" 
				value="change password" onclick="display();return false;" >
				</h:commandButton>
			<h:inputSecret style="margin-right:10px;display:none" id="password"
				value="#{loginbean.password}" binding="#{loginbean.inputPassword}">
				<f:validateLength minimum="1" maximum="25" />
			</h:inputSecret>
			<a4j:commandButton id="cb2"
				style="border: 1px solid #ccc;display:none" immediate="true"
				actionListener="#{loginbean.changePassword}" value="submit"
				oncomplete="display();"></a4j:commandButton>
			<br>
			<br>
			<a4j:commandButton id="submit1" value="view associates"
				action="#{loginbean.view}" style="border: 1px solid #ccc;"
				limitToList="false" reRender="panel"></a4j:commandButton>
			<br>
			<br>

		</h:panelGroup>
		<a4j:outputPanel id="panel" layout="block"
			style="text-align:center;padding-left:33%;" ajaxRendered="true">
			<h:dataTable id="table" var="user" value="#{loginbean.lstUser}"
				headerClass="table_head_css" styleClass="table_list"
				columnClasses="table_column" rendered="#{loginbean.lstUser != null}"
				binding="#{loginbean.dataTable}">
				<h:column>
					<f:facet name="header">
						<h:outputText value="NAME" />
					</f:facet>
					<h:outputText value="#{user.name}" />
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="PHONE" />
					</f:facet>
					<h:outputText value="#{user.phone}" />
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="ADDRESS" />
					</f:facet>
					<h:outputText value="#{user.address}" />
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="DEPARTMENT" />
					</f:facet>
					<h:selectOneMenu id="select" value="#{user.department}" binding="#{loginbean.selectOneMenu}"
						valueChangeListener="#{loginbean.selectValueChange}"
						style="height: auto; width: auto; border: 1px solid #ccc;font-family: Arial;font-size: 12px;"
						onchange="this.form.submit();">
						<f:selectItems value="#{loginbean.lstDepartments}" />
					</h:selectOneMenu>
				</h:column>
			</h:dataTable>
			<h:messages layout="table" style="color:red"></h:messages>
		</a4j:outputPanel>
		<a4j:status>
			<f:facet name="start">
				<h:panelGroup style="padding-left:49%;">
					<h:graphicImage url="/ajax-loader.gif" />
				</h:panelGroup>
			</f:facet>
		</a4j:status>
	</h:form>
</f:view>
</body>
</html>

 

   jar :

  1.  
    1.     commons-beanutils-1.7.0.jar
    2.     commons-collections.jar
    3. commons-digester-1.8.jar
    4. commons-logging.jar
    5. jsf-api.jar
    6. jsf-impl.jar
    7. jstl.jar
    8. ojdbc14.jar
    9. richfaces-api-3.3.2.SR1.jar
    10. richfaces-impl-3.3.2.SR1.jar
    11. richfaces-ui-3.3.2.SR1.jar
    12. spring.jar
    13. tomahawk12-1.1.9.jar