为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我跟着老师的做的,我的在输入正确密码之后也是“用户名或密码错误”

struts.xml
<?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>

    <!--定义常量 -->
    <!-- 禁用动态调用方法 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <constant name="struts.devMode" value="true"></constant>

    <package name="default"  namespace="/"  extends="struts-default">

            <!-- 注册拦截器 -->
            <interceptors>
                    <interceptor name="auth" class="com.imooc.intercepter.AuthIntercepter"></interceptor>
                    <!-- 定义拦截器栈 -->
                    <interceptor-stack name="myStack">
                                     <interceptor-ref name="defaultStack"></interceptor-ref>
                                     <interceptor-ref name="auth"></interceptor-ref>
                    </interceptor-stack>
            </interceptors>

          <!-- 通过此Action访问后台管理页面 ,需要判断用户是否已登录,如果未登录则跳转到登录页面-->
        <action name="auth">
            <result>/WEB-INF/page/manager.jsp</result>
            <result name="login">/lo.jsp</result>
            <!-- 引用自定义的拦截器栈 -->
            <interceptor-ref name="myStack"></interceptor-ref>
        </action>
           
         <action name="login" class="com.imooc.action.LoginAction" method="login">
                    <result name="success">/WEB-INF/page/manager.jsp</result>
                    <result name="error">/lo.jsp</result>
         </action>
    </package>

</struts>

LoginAction.class
package com.imooc.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport  implements   SessionAware{
      
      private   String  uname ;
      private   String  upwd;
      private  Map<String, Object> session;
    
      //获取session 
    public void setSession(Map<String,Object> session) {
                 this.session = session;
    }

    public String getName() {
        return uname;
    }

    public void setName(String name) {
        this.uname = name;
    }

    public String getPassword() {
        return upwd;
    }

    public void setPassword(String password) {
        this.upwd = password;
    }
    
    // 处理login的问题
    public  String  login(){

        if("admin".equals(uname) && "123456".equals(upwd)){
            session.put("loginInfo", uname);
            return  SUCCESS;
        }else{
            session.put("loginError","用户名或密码错误!");
            return   ERROR;
        }
    }   
}
AuthInterceptor.class
public class AuthIntercepter extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
            ActionContext  context = ActionContext.getContext();
            Map<String,Object> session = context.getSession();
            if(session.get("loginInfo")!=null){
                String   result = invocation.invoke();
                return result;
            }else{
                return  "login";
            }
         
    }

}


正在回答

2 回答

https://m.vk.com/@712774205-93415
https://m.vk.com/@712774205-93416
https://m.vk.com/@712774205-93417
https://m.vk.com/@712774205-93418
https://m.vk.com/@712774205-93419
https://m.vk.com/@712774205-93420
https://m.vk.com/@712774205-93421
https://m.vk.com/@712774205-93422
https://m.vk.com/@712774205-93423
https://m.vk.com/@712774205-93424
https://m.vk.com/@712774205-93425
https://m.vk.com/@712774205-93426
https://m.vk.com/@712774205-93427
https://m.vk.com/@712774205-93428
https://m.vk.com/@712774205-93429
https://m.vk.com/@712774205-93430
https://m.vk.com/@712774205-93431
https://m.vk.com/@712774205-93432
https://m.vk.com/@712774205-93433
https://m.vk.com/@712774205-93434
https://m.vk.com/@712774205-93435
https://m.vk.com/@712774205-93436
https://m.vk.com/@712774205-93437
https://m.vk.com/@712774205-93438
https://m.vk.com/@712774205-93439
https://m.vk.com/@712774205-93440
https://m.vk.com/@712774205-93441
https://m.vk.com/@712774205-93442
https://m.vk.com/@712774205-93443
https://m.vk.com/@712774205-93444
https://m.vk.com/@712774205-93445
https://m.vk.com/@712774205-93446
https://m.vk.com/@712774205-93447
https://m.vk.com/@712774205-93448
https://m.vk.com/@712774205-93449
https://m.vk.com/@712774205-93450
https://m.vk.com/@712774205-93451
https://m.vk.com/@712774205-93452
https://m.vk.com/@712774205-93453
https://m.vk.com/@712774205-93454
https://m.vk.com/@712774205-93455
https://m.vk.com/@712774205-93456
https://m.vk.com/@712774205-93457
https://m.vk.com/@712774205-93458
https://m.vk.com/@712774205-93459
https://m.vk.com/@712774205-93460
https://m.vk.com/@712774205-93461
https://m.vk.com/@712774205-93462
https://m.vk.com/@712774205-93463
https://m.vk.com/@712774205-93464
https://m.vk.com/@712774205-93465
https://m.vk.com/@712774205-93466
https://m.vk.com/@712774205-93467
https://m.vk.com/@712774205-93468
https://m.vk.com/@712774205-93469
https://m.vk.com/@712774205-93470
https://m.vk.com/@712774205-93471
https://m.vk.com/@712774205-93472
https://m.vk.com/@712774205-93473
https://m.vk.com/@712774205-93474
https://m.vk.com/@712774205-93476
https://m.vk.com/@712774205-93477
https://m.vk.com/@712774205-93478
https://m.vk.com/@712774205-93479
https://m.vk.com/@712774205-93480
https://m.vk.com/@712774205-93481
https://m.vk.com/@712774205-93482
https://m.vk.com/@712774205-93483
https://m.vk.com/@712774205-93484
https://m.vk.com/@712774205-93485
https://m.vk.com/@712774205-93486
https://m.vk.com/@712774205-93487
https://m.vk.com/@712774205-93488
https://m.vk.com/@712774205-93489
https://m.vk.com/@712774205-93490
https://m.vk.com/@712774205-93491
https://m.vk.com/@712774205-93492
https://m.vk.com/@712774205-93493
https://m.vk.com/@712774205-93494
https://m.vk.com/@712774205-93532
https://m.vk.com/@712774205-93533
https://m.vk.com/@712774205-93534
https://m.vk.com/@712774205-93535
https://m.vk.com/@712774205-93536
https://m.vk.com/@712774205-93537
https://m.vk.com/@712774205-93538
https://m.vk.com/@712774205-93539
https://m.vk.com/@712774205-93540
https://m.vk.com/@712774205-93541
https://m.vk.com/@712774205-93542
https://m.vk.com/@712774205-93543
https://m.vk.com/@712774205-93544
https://m.vk.com/@712774205-93545
https://m.vk.com/@712774205-93546
https://m.vk.com/@712774205-93547
https://m.vk.com/@712774205-93549
https://m.vk.com/@712774205-93550
https://m.vk.com/@712774205-93551
https://m.vk.com/@712774205-93552
https://m.vk.com/@712774205-93553
https://m.vk.com/@712774205-93554
https://m.vk.com/@712774205-93555
https://m.vk.com/@712774205-93556
https://m.vk.com/@712774205-93557
https://m.vk.com/@712774205-93558
https://m.vk.com/@712774205-93559
https://m.vk.com/@712774205-93560
https://m.vk.com/@712774205-93561
https://m.vk.com/@712774205-93562
https://m.vk.com/@712774205-93563
https://m.vk.com/@712774205-93564
https://m.vk.com/@712774205-93565
https://m.vk.com/@712774205-93566
https://m.vk.com/@712774205-93567
https://m.vk.com/@712774205-93568
https://m.vk.com/@712774205-93569
https://m.vk.com/@712774205-93570
https://m.vk.com/@712774205-93571
https://m.vk.com/@712774205-93572
https://m.vk.com/@712774205-93573
https://m.vk.com/@712774205-93574
https://m.vk.com/@712774205-93575
https://m.vk.com/@712774205-93576
https://m.vk.com/@712774205-93577
https://m.vk.com/@712774205-93578
https://m.vk.com/@712774205-93579
https://m.vk.com/@712774205-93580
https://m.vk.com/@712774205-93581
https://m.vk.com/@712774205-93582
https://m.vk.com/@712774205-93583
https://m.vk.com/@712774205-93584
https://m.vk.com/@712774205-93585
https://m.vk.com/@712774205-93586
https://m.vk.com/@712774205-93587
https://m.vk.com/@712774205-93588
https://m.vk.com/@712774205-93589
https://m.vk.com/@712774205-93590
https://m.vk.com/@712774205-93591
https://m.vk.com/@712774205-93592
https://m.vk.com/@712774205-93593
https://m.vk.com/@712774205-93594
https://m.vk.com/@712774205-93595
https://m.vk.com/@712774205-93596
https://m.vk.com/@712774205-93597
https://m.vk.com/@712774205-93598
https://m.vk.com/@712774205-93599
https://m.vk.com/@712774205-93600
https://m.vk.com/@712774205-93601
https://m.vk.com/@712774205-93602
https://m.vk.com/@712774205-93603
https://m.vk.com/@712774205-93604
https://m.vk.com/@712774205-93605
https://m.vk.com/@712774205-93606
https://m.vk.com/@712774205-93607
https://m.vk.com/@712774205-93608
https://m.vk.com/@712774205-93609
https://m.vk.com/@712774205-93610
https://m.vk.com/@712774205-93611
https://m.vk.com/@712774205-93612
https://m.vk.com/@712774205-93613
https://m.vk.com/@712774205-93614
https://m.vk.com/@712774205-93615
https://m.vk.com/@712774205-93616
https://m.vk.com/@712774205-93617
https://m.vk.com/@712774205-93618
https://m.vk.com/@712774205-93619
https://m.vk.com/@712774205-93620
https://m.vk.com/@712774205-93621
https://m.vk.com/@712774205-93622
https://m.vk.com/@712774205-93623
https://m.vk.com/@712774205-93624
https://m.vk.com/@712774205-93625
https://m.vk.com/@712774205-93626
https://m.vk.com/@712774205-93627
https://m.vk.com/@712774205-93628
https://m.vk.com/@712774205-93629
https://m.vk.com/@712774205-93630
https://m.vk.com/@712774205-93631
https://m.vk.com/@712774205-93632
https://m.vk.com/@712774205-93687
https://m.vk.com/@712774205-93688
https://m.vk.com/@712774205-93689
https://m.vk.com/@712774205-93690
https://m.vk.com/@712774205-93691
https://m.vk.com/@712774205-93692
https://m.vk.com/@712774205-93693
https://m.vk.com/@712774205-93694
https://m.vk.com/@712774205-93695
https://m.vk.com/@712774205-93696
https://m.vk.com/@712774205-93697
https://m.vk.com/@712774205-93698
https://m.vk.com/@712774205-93699
https://m.vk.com/@712774205-93700
https://m.vk.com/@712774205-93701
https://m.vk.com/@712774205-93702
https://m.vk.com/@712774205-93703
https://m.vk.com/@712774205-93704
https://m.vk.com/@712774205-93705
https://m.vk.com/@712774205-93706
https://m.vk.com/@712774205-93707
https://m.vk.com/@712774205-93708


0 回复 有任何疑惑可以回复我~

同学,你需要将LoginAction中用于接收用户名和密码的属性名称和login.jsp页面表单中所提交的用户名与密码的input输入框中的name属性名称一致才能够成功利用Struts2的属性驱动来完成属性的封装噢.

注意上面调到的属性名并不是你在LoginAction中声明的字段名称,而是getter/setter方法名称去掉get/set后的名称,首字母小写后需要保证与你在login.jsp页面的表单中的参数名称一致.

例如:

login.jsp

<form action="login.action" method="post" name="loginForm"  >
    用户名:<input type="text" name="username" /><br/>
    密码:<input type="password" name="password" ><br/>
    登录按钮...
</form>

可以看到在表单中两个input标签中的name属性分别为 username password

接下来再看一下LoginAction:

public class LoginAction extends ActionSupport {
    //  利用属性驱动来封装访问该Action中携带的参数
    private String username; // 这边的字段名并不是一定要与表单中的参数名称一致,但是为了满足规范还是建议写成一致的.
    private String password;
    
    //  接下来就是提供set方法来进行参数封装
    //  这里可以看到将set去掉以后那么就只剩下Username了, 然后首字母改为小写就是 username 
    //  这个就是你的属性名称,只有属性名称与请求参数的名称一致才能够利用属性驱动进行赋值噢.
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

建议你去检查一下login.jsp页面中的请求参数的name属性名称和LoginAction中的接收参数的属性名称是否一致.

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我跟着老师的做的,我的在输入正确密码之后也是“用户名或密码错误”

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信