首页 技术 正文
技术 2022年11月20日
0 收藏 986 点赞 4,435 浏览 4205 个字

1.创建如下的目录结构

2、使用Struts2实现登录功能(没有连接数据)

2.在com.entity包下创建

 package com.entity;
/**
* 用户类
* @author Holly老师
*
*/
public class User {
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;
} }

User.java

3.在com.action包下创建LoginAction.java

 package com.action; import com.entity.User;
import com.opensymphony.xwork2.Action;
/**
* 登陆控制类
* @author Holly老师
*
*/
public class LoginAction implements Action {
/**
* action注入实体类对象
* 该对象名和表单里的name属性值的前缀保持一致
* 否则注入不了
*/
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} public String execute() throws Exception {
if(user.getUsername().equals("holly") && user.getPassword().equals("123")){
return "success";
}else{
return "error";
} } // private String user;
// private String pwd;
// public String getUser() {
// return user;
// }
//
// public void setUser(String user) {
// this.user = user;
// }
//
// public String getPwd() {
// return pwd;
// }
//
// public void setPwd(String pwd) {
// this.pwd = pwd;
// }
// public String execute() throws Exception {
// if(user.equals("holly") && pwd.equals("123")){
// return "success";
// }else{
// return "error";
// }
//
// } }

LoginAction.java

4.在src跟目录下创建struts.xml文件

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

struts.xml

5.在WebRoot下编辑index.jsp页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body> <form action="login.action" method="post">
<table>
<tr> <td>
用户名:
</td>
<td>
<input type="text" name="user.username" />
</td>
</tr>
<tr> <td>
密码:
</td>
<td>
<input type="password" name="user.password" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>

index.jsp

6.在WebRoot下创建success.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body>
<h1>登录成功</h1>
欢迎您,<s:property value="user.username"/>
</body>
</html>

success.jsp

7.在webRoot下创建fail.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body>
<h1>登录失败</h1>
用户名或密码错误!
</body>
</html>

fail.jsp

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290