此实例实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面。
一:JSP代码
1:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
User:<input type="text" name="username"><br> Passoword:<input
type="password" name="password"><br> <input
type="submit" value="submit">
</form>
</body>
</html>
2:welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录成功后显示此页面</h1>
<a href="show.action" mce_href="show.action">show</a>
</body>
</html>
3.show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Show This Page 登录后执行此页面
<br>
</body>
</html>
二:Action代码
1:LoginAction
public class LoginAction extends ActionSupport {
private String username;
private String password;
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
@Override
public String execute() throws Exception {
//判断用户名密码是否有输入,如果没有输入就重新回到登陆界面
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
if (this.getUsername().equals("admin")
&& this.getPassword().equals("admin")) {
//登陆成功后将username和passworld的值存入session
ServletActionContext.getRequest().getSession().setAttribute("username",getUsername());
ServletActionContext.getRequest().getSession().setAttribute("password",getPassword());
System.out.println(getUsername());
System.out.println(getPassword());
return SUCCESS;
} else {
return ERROR;
}
}
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;
}
}
2:ShowAction
public class ShowAction extends ActionSupport{
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
三:拦截器代码
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
//取出session的值
String username = (String) ServletActionContext.getRequest().getSession().getAttribute("username");
System.out.println(username);
if (username != null && username.equals("admin")) {
return arg0.invoke();
}else{
return Action.LOGIN;
}
}
}
四:struts配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--name属性:包名,用于被别的包调用或继承 -->
<package name="test" extends="struts-default">
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority" class="com.ly.interceptot.LoginInterceptor"></interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="authority"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/login.jsp</result>
</global-results>
<action name="loginform" class="com.ly.action.LoginFormAction">
<result name="success">/login.jsp</result>
</action>
<action name="login" class="com.ly.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="show" class="com.ly.action.ShowAction">
<result name="success">/show.jsp</result>
<!--使用拦截器 -->
<interceptor-ref name="mydefault"></interceptor-ref>
</action>
</package>
</struts>