图示:

Struts2 登陆小案例_struts


 

配置 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

实体类 User

package entity;

public class User {
	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 say(){
		return "这是一个方法";
	}
}

 

LoginAction Action类

package action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import entity.User;

public class LoginAction implements Action {
	private User user;
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String execute() throws Exception {
		//判断账号密码是否正确
		if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){
			//session
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("USER1", user);
			//request
			Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
			request.put("USER2", user);
			//application
			Map<String, Object> application = ActionContext.getContext().getApplication();
			application.put("USER3", user.getUsername());
			//返回成功
			return SUCCESS;
		}else{
			this.message = "用户名或者密码错误";
			//返回重新输入
			return INPUT;
		}

	}

}

 

配置 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="default" namespace="/" extends="struts-default">
		     <action name="login" class="action.LoginAction" >
		         <result name="success" >welcome.jsp</result>
		         <result name="input" >login.jsp</result>
		    </action>
		</package>
</struts>

 

login.jsp 页面

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>这是登陆页面</title>
</head>

<body>
	<form action="login.action">
		用户名:<input type="text" name="user.username" /><br /> 密码:<input
			type="text" name="user.password" /><br /> <input type="submit"
			value="提交">
	</form>
	<h4>${message }</h4>
	<hr />
	<s:form action="login">
		<s:textfield label="用户名" name="user.username"></s:textfield>
		<s:password label="密码" name="user.password"></s:password>
		<s:submit value="提交"></s:submit>
	</s:form>
	<h4>
		<s:property value="message" />
	</h4>
</body>
</html>

 

welcome.jsp 页面

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>这是欢迎页面</title>
</head>

<body>
	<s:debug></s:debug>
	<h4>This is welcome Page</h4>
	<h4>传统方式拿出来的值</h4>
	<h4>${sessionScope.USER1.username }(从 session 拿出来)</h4>
	<h4>${requestScope.USER2 }(从 request 拿出来)</h4>
	<h4>${applicationScope.USER3 }(从 application 拿出来)</h4>
	<hr />
	<h4>Struts2 Tag</h4>
	<h4>
		<s:property value="#session.USER1.username" />
		(从 session 拿出来,PS可以通过对象拿属性)
	</h4>
	<h4>
		<s:property value="#request.USER2.say()" />
		(从 request 拿出来,PS可以通过对象拿方法)
	</h4>
	<h4>
		<s:property value="#application.USER3" />
		(从 application 拿出来,PS可以直接取到对象)
	</h4>
</body>
</html>

 

效果图:

Struts2 登陆小案例_java_02


 

Struts2 登陆小案例_struts_03


 

Struts2 登陆小案例_web.xml_04


 

Struts2 登陆小案例_xml_05