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, 用户登录完成啦 ,还是很无聊啊 !!!