struts2 学习二 用户登录处理

struts2 学习二 用户登录处理


开发步骤:

1、在WEB工程中添加Login.jsp,编辑页面代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <s:form action="Login.action">
       <s:textfield name="username" label="username" key="username"/>
       <s:password name="password" label="password" key="password"/>
       <s:submit value="submit"/>
   </s:form>
</body>
</html>

2、工程中添加ACTION完成业务处理,WEB工程中添加“LoginAction”,代码片段:

package com.example;

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

public class LoginAction extends ActionSupport {

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

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

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

	public String execute() throws Exception {

		// action 访问Servlet API
		ActionContext ctx = ActionContext.getContext();

		Integer counter = (Integer) ctx.getApplication().get("counter");

		if (counter == null) {
			counter = 1;
		} else {
			counter += 1;
		}

		ctx.getApplication().put("counter", counter);

		ctx.getSession().put("username", getUsername());

		if (getUsername().equals("yzf") && getPassword().equals("123")) {
			ctx.put("tip", "登录成功");
			return SUCCESS;
		} else {
			ctx.put("tip", "登录失败");
			return ERROR;
		}
	}
}

3、在struts.xml中配置action

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 所以action必须放定义在package中 -->
	<package name="example" extends="struts-default">
	    <!-- 定义action的名字 ,及所对应的class -->
	    <action name="HelloWorld" class="com.example.HelloWorldAction">
	         <!--  定义逻辑视图和物理资源之间的映射 -->
			<result name="success">/HelloWorld.jsp</result>
		</action>
		
		<action name="Login" class="com.example.LoginAction">
		    <result name="success">/Main/Welcome.jsp</result>
		    <result name="error">/Main/Error.jsp</result>
		    <result name="input">/Login.jsp</result>
		</action>
	</package>
</struts>

4、添加Welcome.jsp和Error.jsp页面,完成Action跳转

Welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF8"
	pageEncoding="UTF8"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>welcome</h1>
	<br> 访问此时:${applicationScope.counter}
	<br> ${sessionScope.username },${requestScope.tip}
</body>
</html>

Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF8"
	pageEncoding="UTF8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>error</h1>
	访问此时:${applicationScope.counter}
	<br> ${sessionScope.username },${requestScope.tip}
</body>
</html>

5、完成后,启动Tomcat, 在浏览器中输入地址:http://localhost:8080/example/HelloWorld.action 



OK, 用户登录完成啦 ,还是很无聊啊 !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值