Struts2基础(2)_简单的登录

利用Struts2框架设计一个简单的登录页面:

1.在Eclipse中新建Web项目,导入jar包

2.在Web应用下新建web.xml文件(项目下WebContent\WEB-INF\web.xml)

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

	<!-- 定义Struts2的核心Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- 让Struts2的核心Filter拦截所有请求 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

3.新增配置文件properties,路径为:.\src\mess.properties

loginPage=\u767B\u5F55\u9875\u9762
errorPage=\u9519\u8BEF\u9875\u9762
succPage=\u6210\u529F\u9875\u9762
fallTip=\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u4E0D\u80FD\u767B\u5F55
succTip=\u6B22\u8FCE\uFF0C{0}\uFF0C\u60A8\u4EE5\u767B\u5F55
user=\u7528\u6237\u540D
pass=\u5BC6\u7801
login=\u767B\u5F55

4.新增jsp页面,路径:WebContent\login.jsp 、welcom.jsp、error.jsp

 login.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user"></s:textfield>

<s:textfield name="password" key="pass"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
</body>
</html>
welcome.jsp内容如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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><s:text name="succPage"></s:text></title>
</head>
<body>
	<s:text name="succPage"></s:text><hr/>
	本站访问次数为:${applicationScope.counter}<br/>
	${sessionScope.user },您已登录!
	${requestScope.tip }<hr/>
	从cookie中取值:${cookie.user.value }
	
	密码:${cookie.user1.value }
</body>
</html>
5.创建struts.xml文件,放置在src下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- 指定Struts 2配置文件的根元素 -->
<struts>
	<!-- 指定全局国际化资源文件 -->
	<constant name="struts.custom.i18n.resources" value="mess"/>
	<!-- 指定国际化编码所使用的字符集 -->	
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 所有的Action定义都应该放在package下 -->
	<package name="lee" extends="struts-default">
		<action name="login" class="org.crazyit.app.action.LoginAction">
			<!-- 定义三个逻辑视图和物理资源之间的映射 -->		
			<result name="input">/login.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
</struts>

6.创建LoginAction类

package org.crazyit.app.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;

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

public class LoginAction implements Action,ServletResponseAware{
	private String username;
	private String password;
	
	private HttpServletResponse response;
	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(){
		ActionContext ctx = ActionContext.getContext();
		Integer counter = (Integer)ctx.getApplication().get("counter");
		if(counter == null){
			counter = 1;
		}else{
			counter +=1;
		}
		//通过ActionContext设置application范围的属性
		ctx.getApplication().put("counter", counter);
		
		ctx.getSession().put("user", getUsername());
		
		if(getUsername().equals("crazy")
				&& getPassword().equals("leegang")){
			/*ActionContext.getContext().getSession()
			.put("user", getUsername());*/
			
			Cookie c = new Cookie("user", getUsername());
			c.setMaxAge(60*60);
			//获取Cookie的时候需要IE没有点击清除浏览器记录
			response.addCookie(c);
			
			Cookie c1 = new Cookie("user1", getPassword());
			c1.setMaxAge(60*60);
			ServletActionContext.getResponse().addCookie(c1);
			
			ctx.put("tip", "服务器提示:您已成功登录");
			
			return SUCCESS;
		}else{
			ctx.put("tip", "服务器提示:登录失败");
			return ERROR;
		}
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}

 login页面如下:

 

你可能感兴趣的:(java,jsp,struts2)