Struts2 登录界面

真正意义上的拦截,看另外一篇博文

http://blog.csdn.net/larger5/article/details/78643220

下面附上具体的代码

1、JavaBean代码

package bean;

public class Bean {
	public Bean(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	@Override
	public String toString() {
		return "Bean [username=" + username + ", password=" + password + "]";
	}

	public Bean() {
		super();
		// TODO Auto-generated constructor stub
	}

	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;
	}

	private String username;
	private String password;
}
2、Action 代码
package bean;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import bean.Bean;

public class JavaBean extends ActionSupport {
	public Bean getBb() {
		return bb;
	}

	public void setBb(Bean bb) {
		this.bb = bb;
	}

	private Bean bb;

	public String lin() throws Exception {

		if (bb.getUsername().equals("itcast") && bb.getPassword().equals("123")) {

			ServletRequest request = ServletActionContext.getRequest();
			HttpServletRequest re = (HttpServletRequest) request;
			HttpSession session = re.getSession();

			session.setAttribute("username", bb.getUsername());
			session.setAttribute("password", bb.getPassword());

			return "success";
		} else {
			return "fail";
		}
	}
}
3、struts.xml 代码





	
		
			/success.jsp
			/fail.jsp
		
	

4、web.xml 配置代码




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

	
		struts2
		/*
	
5、登录界面 index.jsp 代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix="s" uri="/struts-tags"%>




Insert title here


	

6、登录成功的界面 success.jsp 代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
     <%@taglib prefix="s" uri="/struts-tags"%>




Insert title here


	

用户登录成功!

用户登录信息

使用struts2 property #session

用户名:
密码:

7、登录失败的界面 fail.jsp 代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@taglib prefix="s" uri="/struts-tags"%>



Insert title here


	

用户登录失败!




你可能感兴趣的:(Struts2 登录界面)