struts 2 实现简单的登陆功能
近段时间较空闲,决定把最近新学的一些知识啊稍微理理,作为自己一个完善备份吧。希望能够通过每天空余的时间把一些知识点梳理下。前期的一些东西可能是相当的入门,但也希望从头做起理个清楚吧。
好了闲话少说,从最简单的struts2的基础功能做起把,计划是先做个小登陆系统开始再做一步一步的完善。
1.建立demo-struts2 项目
2.导入struts2必须的几个包
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.9.jar
xwork-2.0.4.jar
3.配置web.xml
源代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Struts 2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts 2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.建立loginAction.java
package com.intohotel.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 7777814042861093731L;
private String username;
private String password;
private Map<String , String> session;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Map<String, String> getSession() {
return session;
}
public void setSession(Map<String, String> session) {
this.session = session;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 类里默认的方法 测试struts.xml里 method未指定的话,进入此默认方法
*/
public String execute(){
System.out.println("=======hello 你进入了struts默认的的登陆方法");
if("heilwolf".equals(this.getUsername().trim()) && "123456".equals(this.getPassword())){
return "success";
}else {
this.addFieldError("username", "username or password error!");
return "failer";
}
}
/**
* 登陆的方法,判断用户名密码 用户名:heilwolf 密码:123456
* @return
*/
public String login(){
System.out.println("=======hello 你进入了heilwolf的登陆方法!");
String name=this.getUsername();
String password=this.getPassword();
if("heilwolf".equals(name.trim()) && "123456".equals(password)){
return "success";
}else {
this.addFieldError("username", "username or password error!");
return "failer";
}
}
/**
* 一个验证器 登陆的时候会先进行验证
*/
public void validate(){
System.out.println("=======hello 你进入了heilwolf重写的验证器!");
if(null==this.getUsername() || "".equals(this.getUsername().trim())){
this.addFieldError("username", "username error");
}
if(null==this.getPassword() || "".equals(this.getPassword().trim())){
this.addFieldError("password", "password error");
}
}
}
5.建立login.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>heilwolf的登陆页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="heilwolf 登陆页面">
<meta http-equiv="description" content="heilwolf 登陆页面">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="message" />
<s:form action="login" namespace="/login">
<s:textfield name="username"/>
<s:password name="password"/>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
6.登陆成功页面result.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="heilwolf 登陆成功了">
<meta http-equiv="description" content="heilwolf 登陆成功了">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
username:${requestScope.username}<br>
password:${requestScope.password}
</body>
</html>
7.struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" namespace="/login" extends="struts-default">
<action name="login" method="login" class="com.intohotel.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
<result name="failer">/login.jsp</result>
</action>
</package>
</struts>
好了 项目能跑起来 用户名heilwolf 密码123456 史上最简单的一个登陆系统可以运行了 ,先吃饭了,下午把拦截器这些东西再加上