Struts2实现登录,最简单的struts2入门

先把项目目录放出来:
在这里插入图片描述
上代码!
loginAction.java代码:

package loginAction;
import loginBean.loginBean;
public class LoginAction {
	private String userName;
	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;
	}
	//处理用户登录的方法
		public String execute() throws Exception{
			loginBean lb=new loginBean();
			boolean b=false;
			if (lb.login(userName,passWord)) {
				return "success";
			}else {
				return "error";
			}
		}
}

loginBean.java代码:

package loginBean;
public class loginBean {
	private String userName;
	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;
	}
	//处理用户登录的方法
	public boolean login(String userName,String passWord) {
		boolean b=false;
		if (userName.equals("qq")&&passWord.equals("123")) {
			b=true;
			return b;
		}else {
			b=false;
			return b;
		}	
	}
}

struts.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="lwn" extends="struts-default">
		<action name="login" class="loginAction.LoginAction">
		<result name="error">/login/login.jsp</result>
		<result name="success">/login/success.jsp</result>
		</action>
	</package>
</struts>

login.jsp代码:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>基于Struts2的登录系统应用</title>
</head>
<body>
<form method="post" action="login">
用户名:š<input name="userName" type="text" size=26>
<br>
密码:<input name="passWord" type="text" size=26>
<br>
<input type="submit" value="登录">
</form>
<hr>
</body>
</html>

success.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>登录成功界面</title>
</head>
<body>
<H1>你登录成功,欢迎你!</H1>
</body>
</html>

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>pro1</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置struts核心控制器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值