一、建立一个名叫jsp_struts2的项目
二、导入jar包
如上图:jar包导入在WebContent/WEB-INF/lib下
三、建立一个LoginAction类
LoginAction类的set方法取得登录界面的登录名和登录密码
execute()方法是程序的入口
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() { System.out.println(username+" "+password); if ("123456".equals(username) && "123456".equals(password)) { return "SUCCESS"; } return "INPUT"; } }
四、建立界面(登录失败返回登录界面)
1、建立登录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>登录界面</title> </head> <body> <form action="LoginAction" method="post"> 用户名:<input type="text" name="username" /><br /> 密码:<input type="password" name="password" /><br /> <input type="submit" value="提交" /> </form> </body> </html>
2、建立登录成功success.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>登录成功</title> </head> </head> <body> 登录成功,欢迎你, </body> </html>
五、配置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> <package name="default" namespace="/" extends="struts-default"> <action name="LoginAction" class="nsu.edu.student.action.LoginAction"> <result name="SUCCESS">/success.jsp</result> <result name="INPUT">/login.jsp</result> </action> </package> </struts>