搭建一个简单struts2框架的登陆
第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252
出于学习的目的,可以把整个完整的压缩文件都下载下来。
里面包括:1 apps:示例应用,对学习很有帮助 ; 2 docs:相关学习文档、API文档等; 3 lib:核心类库,依赖包; 4:src:源代码
第二步:在eclipse新建一个Dynamic Web Project类型工程,一直点next,记得勾选generate web.xml deployment descriptor,才能有web.xml
第三步:导入jar包
struts2有个几个必备的jar包,可以去官网下载struts-min-lib.zip,里面的包就是必须要的jar包。
第四步:编写对应的Action
在src下面新建一个包com.struts.action(注意:命名需要时com.XXX.action的形式),然后再下面新建一个java文件:LoginAction.java
package com.struts.action; import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
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
{
if("username".equals(this.getUsername()) && "password".equals(this.getPassword()))
{
return "success";
}
else
{
return "fail";
} }
}
第五步:新建结果页面
在WEB-INF新建下面两个文件:
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>success!</title>
</head>
<body>
success!
</body>
</html>
fail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>fail!</title>
</head>
<body>
fail!
</body>
</html>
第六步:在WEB-INF下新建初始页面(根据LoginAction知道需要提交的表单里面有两个参数)
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
第七步:配置在WEB-INF下的web.xml:1、加入struts过滤器 2、配置首页
<?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>struts2Project</display-name>
<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>
</filter-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/login.html</welcome-file>
</welcome-file-list>
</web-app>
第八步:配置struts2配置文件,在src目录下新建struts.xml,进行action配置
<?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"> <!-- START SNIPPET: xworkSample -->
<struts>
<package name="com.struts.action" extends="struts-default">
<action name="login" class="com.struts.action.LoginAction">
<result name="success">/WEB-INF/success.jsp</result>
<result name="fail">/WEB-INF/fail.jsp</result>
</action>
</package>
</struts>
结尾:至此完成简单struts2框架的登陆搭建,放进tomcat并启动,就可以通过http://localhost:8080/struts2Project/ 访问。
搭建一个简单struts2框架的登陆的更多相关文章
- vue+vux-ui+axios+mock搭建一个简单vue框架
1.首先感谢同事 2.之前一直在做angularjs的项目,目前vue火热,所以自己搭建了一个的vue框架,在此作为记录 vue+vux-ui这里就不介绍了,有很多博客都写的很详细了. 下面简单记录下 ...
- Prism for WPF 搭建一个简单的模块化开发框架(一)
原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...
- 用express搭建一个简单的博客系统
转自:https://blog.csdn.net/qq_29721837/article/details/62055603 Express 简介 Express 是一个简洁而灵活的 node.js W ...
- 【netty】(2)---搭建一个简单服务器
netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单
原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...
- Prism for WPF 搭建一个简单的模块化开发框架(二)
原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...
- 【转】使用webmagic搭建一个简单的爬虫
[转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
随机推荐
- 对象比较器:Comparable和Comparator
在进行对象数组排序的过程中需要使用到比较器,比较器有两个:Comparable和Comparator ①.java.lang.Comparable:是在类定义是时候默认实现好的接口,里面提供有一个co ...
- AC日记——中位数 洛谷 P1168
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- 数据库_MYSQL
数据库简介 数据库分类:关系型数据库.非关系型数据库 常用的关系型数据库有:orcale .mysql .sql server等等 常用的非关系型数据库有: Memcached.redis.mong ...
- time.c 的Java实现(从timestamp计算年月日时分秒等数值)
time.c的Java实现 public class GMT { public static final int EPOCH_YEAR = 1970; public static final int[ ...
- SQL获取本周、本月、本季度的记录的语句
前提条件:假设表名为:tableName:时间字段名为:theDate ①查询本周的记录 select * from tableName where DATEPART(wk, theDate) = D ...
- javaScript中的页面传值
function getURIParam(name) { var search = window.location.search; search = search.substring(1); if ( ...
- linux perf - 性能测试和优化工具
Perf简介 Perf是Linux kernel自带的系统性能优化工具.虽然它的版本还只是0.0.2,Perf已经显现出它强大的实力,足以与目前Linux流行的OProfile相媲美了. Perf 的 ...
- 一行代码解决ie6,7,8,9,10兼容性问题
"浏览器模式"."文档模式"选项的区别如下: 1."浏览器模式"用于切换IE针对该网页的默认文档模式.对不同版本浏览器的条件备注解析.发送给 ...
- C#接口和抽象类的区别
大家都容易把这两者搞混,我也一样,在听李建忠老师的设计模式时,他也老把抽象类说成接口,弄的我就更糊涂了,所以找了些网上的资料. 一.抽象类: 抽象类是特殊的类,只是不能被实例化:除 ...
- Asp.Net MVC<七>:Model
Model 指ViewModel ,其作用: 用于目标Action的参数绑定 用于View呈现 Model元数据则是对ViewModel数据类型的描述,其作用在于控制作为Model的数据对象在View ...