struts2的登陆例子源码

本文介绍使用Struts2框架实现用户登录的具体步骤。包括搭建项目环境、配置struts.xml及web.xml文件、编写Action类等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STRUTS2做登陆的简单实现

 

第一步:创建一个WEB项目.

 

第二步:倒入STRUTS2的.jar包.

 

第三步:在src下创建一个struts.xml文件:

 

<struts>

- < package name =" myPackage " extends =" struts-default ">
- <!--
 login begin 
  -->
- < action name =" forwardLogin " method =" forward " class =" com.sinoest.login.action.LoginAction ">
  < result > /Login.jsp </ result >
  </ action >
- < action name =" login " class =" com.sinoest.login.action.LoginAction ">
  < result name =" input "> /Login.jsp </ result >
  < result > /Welcome.jsp </ result >
  </ action >
- <!--
 login end 
  -->
  </ package >
  </ struts >
第四步:配置web.xml文件:
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <jsp-config>
  <taglib>
   <taglib-uri>struts-tags</taglib-uri>
   <taglib-location>
    /WEB-INF/struts2-core-2.0.14.jar/struts-2.0.dtd
   </taglib-location>
  </taglib>
 </jsp-config>
第五步:写ACTION文件:
package com.sinoest.login.action;
 
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
 
public class LoginAction extends ActionSupport {
    /**
  *
  */
 private static final long serialVersionUID = 7315440691690047462L;
 private String name;
    private String password;
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getPassword() {
       return password;
    }
    public void setPassword(String password) {
       this.password = password;
    }
   
    public String forward() throws Exception {
       return SUCCESS;
    }
   
    @Override
    public String execute() throws Exception {
       if("admin".equals(name)) {
       HttpSession session = ServletActionContext.getRequest().getSession();
           session.setAttribute("name", name);
           return SUCCESS;
       } else {
           return INPUT;
       }
    }
}
第六步:创建/login.jsp和/welcome.jsp文件.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值