实验二 Struts2核心组件的应用
一、目的与任务
目的:学习Action
中的动态方法调用和Action
对Servlet API
的三种访问方法,学习OGNL
表达式和Struts2
标签的使用
任务:实现基于Struts2
的登录和注册系统,系统中练习使用Action、OGNL
表达式和Struts2
标签
二、内容、要求与安排方式
1、实验内容与要求:
(1)采用指定method
属性方式实现调用Action
中的不同方法,分别实现登录和注册功能;
在登录成功时,通过ActionContext
访问session对象,并将用户名存储到session
对象中;
在注册成功时,通过辅助类获取session
对象,并将用户信息存储到session
对象中;
最终在页面上显示用户名以及登录成功或者注册成功的信息。
(2)使用Struts2
标签实现原来的注册页面。
(3)修改注册成功页面使用OGNL
表达式显示所有注册信息。
2、实验安排方式:上机编码。
三、源代码如下
-
项目结构:
-
源代码:
structs.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.5.dtd"> <struts> <package name="default" namespace="/" extends= "struts-default"> <action name="login" class= "action.LoginRegisterAction" method="login"> <result name="success">/success.jsp</result> <result name="login">/login.jsp</result> </action> <action name="register" class="action.LoginRegisterAction" method="register"> <result name="success">/success.jsp</result> <result name="register">/register.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding= "UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
login.jsp
<%-- Created by IntelliJ IDEA. User: Lunatic Date: 2020/5/18 Time: 11:42 To change this template use File | Settings | File Templates. --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎进入登录注册界面</title> <style type="text/css"> body { background-color: darkturquoise; font-size: 25px; text-align: center; } </style> </head> <body> <form action="login.action" method="get"> 用户名:<label> <input type="text" name="user.name"> </label><br> 密码: <label> <input type="password" name="user.pass"> </label><br> <button type="submit" align="center">登录</button> <a href="register.jsp"> <button type="button" align="center">注册</button> </a> </form> </body> </html>
register.jsp
<%-- Created by IntelliJ IDEA. User: Lunatic Date: 2020/5/18 Time: 12:13 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>欢迎进入登录注册界面</title> <style type="text/css"> body { background-color: cornflowerblue; font-size: 25px; text-align: center; vertical-align: middle; } </style> </head> <body> <form action="register.action" method="get"> 用户名: <label> <input type="text" name="user.name"> </label><br> 密码: <label> <input type="password" name="user.pass"> </label><br> </label> <button type="submit" align="center">注册并登陆</button> </form> </body> </html>
success.jsp
<%-- Created by IntelliJ IDEA. User: Lunatic Date: 2020/5/19 Time: 11:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>登陆注册成功</title> </head> <body> <s:if test='#session.type=="login"'> <h1>登录成功,您的信息如下: </h1> 登录名: <s:property value="user.name"/><br> </s:if> <s:elseif test='#session.type=="register"'> <h1>注册成功,您的信息如下:</h1> 登录名: <s:property value="user.name"/><br> </s:elseif> <s:else> <% response.sendRedirect("fail.jsp"); %> </s:else> </body> </html>
fail.jsp
<%-- Created by IntelliJ IDEA. User: Lunatic Date: 2020/5/19 Time: 15:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>很抱歉,登陆失败</title> <style type="text/css"> body { background-color: red; font-size: 25px; text-align: center; vertical-align: middle; } </style> </head> <body> <h1>请检查登录名或密码是否正确</h1> </body> </html>
Login_Register.java
package Action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class LoginRegisterAction implements Action { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String login() { if(getUser() != null) { if(getUser().getName().equals("Lunatic")&&getUser().getPass().equals("000000")&& getUser().getPass().trim().length() > 0) { ActionContext.getContext().getSession().put("type", "login"); return SUCCESS; } } return LOGIN; } public String register() { if(getUser() != null) { if(getUser().getName() != null && getUser().getName().trim().length() > 0) { ActionContext.getContext().getSession().put("type", "register"); return SUCCESS; } } return LOGIN; } @Override public String execute() throws Exception { return null; } }
User.java
package Action; public class User { private String name; private String pass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
-
四、实验结果
登录:
登录成功:
注册:
注册成功:
登录失败: