struts2 简单登录校验示例

一、实验目的

理解并掌握业务控制类创建的不同方式

理解struts.xml中action配置各属性的作用以及result配置参数作用

二、实验内容

采用不同业务控制类定义方式实现Login原型

深入理解struts.xml配置文件作用

三、 实验步骤及代码

采用struts2框架实现Login原型,业务控制类实现方式:直接实现(CheckAccountCheckAccountI)、

实现Action接口(CheckAccountII)、继承父类ActionSupport(CheckAccountIIII)

login.jsp


  	
UserName:
PassWord:

CheckAccount.java

package com.util.login;
public class CheckAccount {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

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

	public String execute(){
		if(username.equals("zhangsan") && password.equals("123")){
			return "success";
		} else {
			return "error";
		}
	}
}

CheckAccountI.java (JavaBean封装)

package com.util.login;

public class CheckAccountI {
	private Account account;
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	public String execute(){
		if(account.getUsername().equals("zhangsan") && account.getPassword().equals("123")){
			return "success";
		} else {
			return "error";
		}
	}
}

CheckAccountII.java (实现Action接口)

package com.util.login;
import com.opensymphony.xwork2.Action;
public class CheckAccountI implements Action {
	private Account account;
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}

	public String execute() throws Exception {
		if(account.getUsername().equals("zhangsan") && account.getPassword().equals("123")){
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
}


CheckAccountIII.java (继承ActionSupport)

package com.util.login;
import com.opensymphony.xwork2.ActionSupport;
public class CheckAccountII extends ActionSupport{
	private Account account;
			
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}

	@Override
	public String execute() throws Exception {
		if(account.getUsername().equals("zhangsan") && account.getPassword().equals("123")){
			return "success";
		} else {
			return "error";
		}
	}
}

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Login.jsp' starting page
    
	
	
	    
	
	
	

  
  <%@taglib uri="/struts-tags" prefix="s" %>
  
  	
用户登录:
用户名:
密码:

welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




  
    
  
  
  
    登录成功!!!! ${name}


配置文件
web.xml


  	
  
    index.jsp
  
  
  	struts2
  	
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	
  
  
  	struts2



	
		
			/welcome.jsp
			/Login.jsp
		
	
    

/* This is the description of my J2EE component This is the display name of my J2EE component checkAction com.tj.struts.demo.checkAction checkAction /checkAction
 
   

sttuts.xml




	
		
			/welcome.jsp
			/Login.jsp
		
	age>
    



你可能感兴趣的:(struts)