struts2框架完成用户登录的案例

1.用框架的知识完成简单的用户登录的案例

思路:我们会创建一个jsp页面,里面会有用户名和密码的输入框,当我录入完信息后会向服务器发送请求
在servlet服务器里面要做几件事:1.获取请求参数(username、password) 2. 判断用户名和密码是否正确
3. 如果正确就跳转到success.jsp页面,如果不正确就跳转到failer.jsp页面

该案例用struts框架做,难点在于:(下面三个问题解决,就对struts2框架有个最基本的认识)
1. 我们在服务器怎么样获取参数
2. 如何跳转页面
3. struts2框架中使用什么来代替原来的servlet完成逻辑操作

[1] 导入13个jar包
在资料里面struts-2.3.24-all –> apps –> struts2-blank.war 解压 –> 得到里面的13个jar包
放到项目的lib文件夹下

[2] 配置文件
在web.xml里面配置一个StrutsPrepareAndExecuteFilter

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>sturts2-day1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

    <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>

[3] 在WebContent 下面创建一个jsp文件,起名叫login,jsp,

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
    <!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>Insert title here</title>
    </head>
    <body>
<form action="${ pageContext.request.contextPath }/login" method="post">
    账号:<input type="text" name="username"></br>
    密码:<input type="password" name="password"></br>
    <input type="submit" value="登录">
</form>

[4] 创建一个登陆成功的jsp,用做用户登录成功后跳转的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
    <h1>登录成功</h1>
</body>
</html>

[5] 创建一个登陆失败的jsp,用做用户登录失败后跳转的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
    <h1>登录失败</h1>
</body>
</html>

[6] 创建一个struts.xml 在src下面创建

<?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="com.itheima.action.LoginAction" method="login">
//上面的<form>表单中路径是/login ,我们在xml文件里面的namespace 和  name="login" 二者加在一起就是上面的路径,系统就知道
//是/login 路径下的 在LoginAction类下的login的方法,进行执行
    <result name="success">/success.jsp</result>
    <result name="failer">/failer.jsp</result>
</action>
</package>
</struts>

//注意:前面四行可以粘贴复制,但是会报错,需要配置struts-2.3.dtd 这个文件,具体步骤如下
//在eclipse最上面的 window –> Preferences –> 在输入框中输入xml –> xml.Catalog –> 右边的add按钮,
//找到2.3.dtd 文件(E:/合肥黑马程序员/黑马课程框架阶段/框架阶段/06_第六阶段/01_Struts/Struts_day01/
//资料/struts-2.3.24-all/struts-2.3.24/src/core/src/main/resources/struts-2.3.dtd)
//下面的keytype 框选择 URI key的框里面就把struts.xml里面的http://struts.apache.org/dtds/struts-2.3.dtd
//把这个2.3.dtd的地址路径粘贴过去即可完成配置,就不会报错了

[7] 最后创建一个LoginAction的servlet

package com.itheima.action;

public class LoginAction {
private String username; //直接在Action类中提供成员属性,并为其提供get/set方法。就可以得到请求参数
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;
}

/*
 *  登录操作 本来需要在数据库中查找,这里面暂时就不需要了么直接给定username 和 password
 */
public String login(){
    //判断比较
    if("tom".equals(username) && "111".equals(password)){  //这里面的username和password一定要和上面private私有的值一样
        //正确
        return "success";
    }else{
        //错误
        return "failer";
    }

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值