【SSH高速进阶】——struts2简单的实例

近期刚刚入门struts2。这里做一个简单的struts2实例来跟大家一起学习一下。

本例实现最简单的登陆,仅包括两个页面:login.jsp 用来输入username与password;success.jsp 为登陆成功页面。error.jsp为登陆失败页面。


1、新建web项目“struts2”


2、引入jar包


下载struts2所需jar包 struts-2.3.24-all.zip

解压后将例如以下最主要的jar包导入到WebRoot/WEB-INF/lib下

struts2基本jar包

3、在WebRoot下建立页面


login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head></head>  <body><form action="login">username:<input type="text" name="username" /><br>password:<input type="password" name="password" /><br><input type="submit" value="submit" /><br></form>   </body>
</html>

此处form标签的action属性值指向的是以下struts.xml中的action标签的name属性,表示提交的表单由action标签中的class指向的LoginAction类来处理。

success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head></head><body>恭喜您:${requestScope.username} 登陆成功<br/></body>
</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head></head> <body>登陆失败</body>
</html>

3、配置web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name> <welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><!-- 此Filter目的是设置全部的请求都由Struts2来处理 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern><!-- 全部请求都由Struts2来处理 --></filter-mapping>
</web-app>

此处用filter过滤器设置了不论什么client的请求都由struts2来处理(设置struts为请求分发器)

4、建立action


package com.danny.user.action;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() {if("admin".equals(username) && "admin".equals(password)){return "success";}else{return "error";}}
}

这个LoginAction用于处理登陆页面发来的数据。既然要处理数据,首先要接收数据,那么它是怎样接收到前台的数据的呢?
页面提交之后,去struts.xml中寻找相应action,进而找到LoginAction。LoginAction依据表单中input的name属性来获取值。规则就是:input的name属性值要和LoginAction中相相应的get、set方法后面的值一样。与LoginAction的字段没有关系:比方input的name属性值为“username”,那么LoginAction重相应的字段的get方法名应为“getUsername()”。所以这个LoginAction全然能够改成这样:

package com.danny.user.action;public class LoginAction {private String name;private String pwd;public String getUsername() {return name;}public void setUsername(String username) {this.name = username;}public String getPassword() {return pwd;}public void setPassword(String password) {this.pwd = password;}public String execute() {if("admin".equals(name) && "admin".equals(pwd)){return "success";}else{return "error";}   }
}

LoginAction中的execute方法是每一个action必需要实现的方法,用于处理数据并返回要显示的界面。

5、配置struts.xml


在src文件夹下建立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="struts2" extends="struts-default"> <!-- extends是继承的意思,struts-default位于struts2-core-2.3.24.jar.struts-default.xml --> <action name="login" class="com.danny.user.action.LoginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>

struts.xml中,每一个action相应一个action。这个name为”login”的action就相应com.danny.user.action包下的LoginAction。result的name相应于action的execute的返回值,假设返回”success”则跳转到success.jsp;假设返回”error”则跳转到error.jsp。


至此。部署项目,訪问localhost:8080/struts2,进入登陆页面:

登陆页面

登陆成功时,跳转到成功页面:

登陆成功页面

用户名、密码错误时,跳转到登陆失败页面:

登陆失败页面



整个步骤例如以下

这里写图片描写叙述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278474.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《智能家居》培训第六天------2019-01-10

目录&#xff1a; 一&#xff09;摄像头 二&#xff09;照明 三&#xff09;所想 四&#xff09;总结 一&#xff09;摄像头 摄像头这块学了跟没学一样我觉得&#xff0c;摄像头给的api&#xff0c;yuyv转rgb24也是给的api&#xff0c;总而言之就是&#xff0c;直接给了两个源文…

在Linux上按大小列出文件和目录

This page will show us how to create a list of files and folders ordered by size using standard Linux commands. 该页面将向我们展示如何使用标准Linux命令创建按大小排序的文件和文件夹列表。 命令 (Command) To get a list with the size of each item in a folder, y…

记一次kafka数据丢失问题的排查

2019独角兽企业重金招聘Python工程师标准>>> 数据丢失为大事&#xff0c;针对数据丢失的问题我们排查结果如下。 第一&#xff1a;是否存在数据丢失的问题&#xff1f; 存在&#xff0c;且已重现。 第二&#xff1a;是在什么地方丢失的数据&#xff0c;是否是YDB…

Maximum upload size exceede上传文件大小超出解决

在这里记录三种方法, 努力提高自己的姿势水平 application.yml配置spring:servlet:multipart:enabled: truemax-file-size: 10MB #单个文件最大大小max-request-size: 1024MB #上传数据总大小 application.properties配置spring.servlet.multipart.max-file-size10Mb #单个文件…

ipad iphone开发_如何在iPhone或iPad上更改应用程序的语言

ipad iphone开发BigTunaOnline/Shutterstock.comBigTunaOnline / Shutterstock.comApple’s iOS 13 makes the iPhone and iPad multilingual. Now, you can change the language of an individual app without changing your primary system language. Each app can have its …

Docker最全教程——从理论到实战(七)

Docker最全教程——从理论到实战&#xff08;七&#xff09; 原文:Docker最全教程——从理论到实战&#xff08;七&#xff09;在本系列教程中&#xff0c;笔者希望将必要的知识点围绕理论、流程&#xff08;工作流程&#xff09;、方法、实践来进行讲解&#xff0c;而不是单纯…

Bash Cookbook 学习笔记 【中级】

Read Me 本文是以英文版<bash cookbook> 为基础整理的笔记&#xff0c;力求脱水2018.01.21 更新完【中级】。内容包括工具、函数、中断及时间处理等进阶主题。本系列其他两篇&#xff0c;与之互为参考 【基础】内容涵盖bash语法等知识点。传送门【高级】内容涉及脚本安全…

设置Windows 10时如何创建本地帐户

Windows 10 tries its hardest to make you use a Microsoft account. The option was already hidden, but now it’s not even offered on Windows 10 Home while you’re connected to the internet. Here’s how to create a local account anyway. Windows 10尽最大努力使…

HSQL

Hive的数据存储  1、Hive中所有的数据都存储在 HDFS 中&#xff0c;没有专门的数据存储格式&#xff08;可支持Text&#xff0c;SequenceFile&#xff0c;ParquetFile&#xff0c;RCFILE等&#xff09;  2、只需要在创建表的时候告诉 Hive 数据中的列分隔符和行分隔符&…

在PowerPoint 2010中将鼠标用作激光笔

Have you ever wished you had a laser pointer to focus attention on a key point in a PowerPoint slideshow? Today, we’ll take a look at how can use use your mouse as a laser pointer in PowerPoint 2010. 您是否曾经希望激光指示器能将注意力集中在PowerPoint幻灯…

Java 8 并发: 原子变量和 ConcurrentMap

原文地址: Java 8 Concurrency Tutorial: Atomic Variables and ConcurrentMap AtomicInteger java.concurrent.atomic 包下有很多原子操作的类。 在有些情况下&#xff0c;原子操作可以在不使用 synchronized 关键字和锁的情况下解决多线程安全问题。 在内部&#xff0c;原子类…

this表示当前对象简单实例

直接上代码 class Message { private Channel channel ; // 保存消息发送通道 private String title ; // 消息标题 private String content ; // 消息内容 // 4、调用此构造实例化&#xff0c;此时的channel 主类ch public Message(Channel channel,String title,String cont…

twitter推文不收录_如何使用Twitter书签保存推文供以后使用

twitter推文不收录Khamosh PathakKhamosh PathakTwitter has a new Bookmarks feature that lets you privately save tweets for later. If you’ve been using the Like feature as a workaround for saving tweets, here’s why you should start bookmarking. Twitter具有一…

if的作用域问题 *输出1~6的随机数*

1 //测试if语句2 public class TestIf {3 public static void main(String[] args){4 double d Math.random();//0~1之间的小数5 int e (int)(d*5); //[0,4]6 //int f 1(int)(d*6); //[1,6] 掷色子7 System.out.println(e);8 …

为您的Blogger博客设计一个美丽的新主题

Would you like to give your Blogger blog a fresh coat of paint with a new theme? Here’s how you can use the new Template Designer to make your Blogger site stand out from the crowd and look great. 您想给Blogger博客一个新的主题吗&#xff1f; 您可以通过以…

Lab 6-4

In this lab, we’ll analyze the malware found in the file Lab06-04.exe. Questions and Short Answers What is the difference between the calls made from the main method in Labs 6-3 and 6-4? A: The function at 0x401000 is the check Internet connection method…

步入三十岁前的总结:看似经历很多得到很多,但,实际却一无所得

本文算是一篇审视自己的文章吧&#xff0c;感觉跟我类似经历的人应该很多&#xff0c;认同感应该也大一些。我是12年网络专业很普通的一所大专院校毕业&#xff0c;到现在为止工作已经超过五年。这五年里&#xff0c;做过运维工程师&#xff0c;也在小车床工作间里做了一下技工…

vue---day03

1. Vue的生命周期 - 创建和销毁的时候可以做一些我们自己的事情 - beforeCreated - created - beforeMount - mounted - beforeUpdate - updated - activated - deactivated - beforeDestroy - destroyed 1.1 知识点回顾 1.1.1 be…

U Sparkle 开发者计划招募中!

向我们投稿吧 在此之前&#xff0c;我们有收到过几篇民间高手的投稿&#xff0c;如&#xff1a; USequencer 初识&#xff08;作者&#xff1a;焱燚(七火)&#xff09; Unity游戏界面解决方案: PSD For UGUI&#xff08;作者&#xff1a;张俊钦&#xff09; UGUI 降低填充率技巧…

阶乘和 大整数

///大整数阶乘的和 #include<bits/stdc.h> using namespace std; int main() {int n;while(cin>>n){int a[2000] {1},b[2000] {0}; //存放结果的数组a。int c; //b用于存放每位存放的结果。int r0; //r用来表示进位的数。int h1,hb1; //h用来表示运算过程中 结果a…