先把项目目录放出来:
上代码!
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>