【Struts2】Struts2框架的搭建
1,Struts2简介
struts1和struts2都是由Apache组织发布的,但是比较有趣的是struts2和struts1并没有“血缘关系”。在Apache发布struts1之后,当时是还是非常流行的,但是随着时间推荐,这时候struts1暴露的问题就越来越多了,在这个时候opensymphony组织发布了WebWork框架,WebWork框架在当时适应的很好。opensymphony组织由于向外推广的力度不够,使得WebWork不能很快的面向大众。这个时候Apache组织联合opensymphony组织发布了struts2,因此struts2和struts1联系并不大。在struts2刚发布出来的时候,许多的类库都是直接使用struts1的。
2,Struts2和SpringMVC对比
struts2和springMVC结构非常相似,都是基于MVC结构的。就连配置都很相似,关于SpringMVC和Spring的知识可以参见http://www.cnblogs.com/HDK2016/category/918254.html。
下面给两种图片,分别是springMVC和struts2的结构流程图。
SpringMVC流程图:

struts2流程图:

通过这两张图片的对比可以看出。
struts2中的 StrutsPrepareAndExecuteFilter 相当于 SpringMVC中的 DispatcherServlet。
struts2中的 Action 相当于 SpringMVC 中的 Controller
struts2中的 result 相当于 SpringMVC 中的 ViewResovler
3,Struts2框架的搭建
首先应该下载jar包,将jar包导入到项目的lib目录下面。

web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>struts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
</web-app>
web.xml
由于struts2默认只处理有以.action结尾或是无后缀的url,所以计算web.xml中过滤器不过滤url,也会有url的限制,一般在struts2项目中不再web.xml中过滤url。web.xml会默认在加载src目录下的struts.xml文件。注意文件名称不要改变,否则的话也可以在web.xml中指定struts.xml的位置。
struts.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts>
<!--Struts默认只会通过.action和无后缀的请求,我们可以通过指定extension来使得Struts只通过.do的URL的请求。-->
<constant name="struts.action.extension" value="do"/> <!-- 必须继承struts-default,才能使用Result组件 -->
<package name="user" extends="struts-default">
<!-- name请求名;class是Action类名;method是Action方法名,默认execute -->
<action name="userlogin" class="cn.test.controller.UserLogin">
<!-- name对应action返回值;type指定Result 类型 -->
<result name="result" type="dispatcher">loginResult.jsp</result>
</action>
</package>
</struts>
struts.xml
我们在struts.xml文件中使用<constant>覆盖了默认了文件扩展名,使得只处理以.do的url,如果想接受多个,那么以逗号隔开。比如: value="do,action,," ,就可以处理以.do,.action和无后缀的url了。
UserLogin.java 文件
package cn.test.controller;
public class UserLogin {
/*
*struts中规定,接受页面传过来的值和发送到页面中值都通过声明全局变量,然后通过setter和getter方法完成。
*/
private String username;//名字可要页面传过来的值一致,否则不能正确接受值
private String password;
private String resultmessage;//返回值
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 getResultmessage() {
return resultmessage;
}
public void setResultmessage(String resultmessage) {
this.resultmessage = resultmessage;
}
public String execute(){
if("张三".equals(username) && "123456".equals(password)){
resultmessage="登录成功";
}else{
resultmessage="登录失败";
}
return "result";
}
}
UserLogin.java
Struts2规定,从页面接受值和传送值到页面都是通过全局变量来实现的。然后控制器中方法的返回值只有两种,一种是Sting,另一中就是void。
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="userlogin.do" method="POST"> 用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/>
</form>
</body>
</html>
login.jsp
loginResult.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>
${resultmessage}
</body>
</html>
loginResult.jsp
到这里我们就配置一个简单的用户登录的struts2框架了。
【Struts2】Struts2框架的搭建的更多相关文章
- 【Spring】Spring+struts2+Hibernate框架的搭建
1.搭建过程 首先需要引入Spring.Struts2.Hibernate的开发包,已经数据库的驱动包. UserAction.java文件 package cn.shop.action; impor ...
- Struts2+Spring+Hibernate(SSH)框架的搭建
首先需要下载struts2 ,spring4,hibernate5 的资源包; struts2资源包下载路径:http://www.apache.org/spring资源包下载路径:http://p ...
- Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型
- struts2.1笔记05:struts2开发环境的搭建
1.找到开发Struts应用需要使用到的jar文件. 首先我们要在myEclipse中新建一个Web Project,我们这里命名为"struts2".然后我们就要使用jar文件, ...
- spring+struts2+ibatis 框架整合以及解析
一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...
- struts2.5框架使用通配符指定方法常见错误
struts2.5框架使用通配符指定方法(常见错误) 在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace ...
- 关于struts2 验证框架在联网的时候可以用,不联网不起作用的问题
这是一个让我很头痛的问题,我是在一个其他的项目框架的基础上来开发新的项目. 当使用struts验证框架时,突然发现这个验证不起作用了,我就纳闷了之前用这个开发的项目好好的怎么到我这就不能用了呢? xm ...
- struts2笔记01-环境搭建
1.官网下载struts2 struts-2.3.28-all.zip,这个包可谓应有尽有,以后全靠它了! 2.jar包怎么选? (1)struts-2.3.28-all\struts-2 ...
- Struts2+Hibernate框架探险
写这篇文章的目的 了解 JavaWeb 开发的人都知道SSH和SSM框架,前段时间开始接触 JavaWeb 开发,看了几个教学视频后就想上手构建一个小型 Web项目,可在跟着视频敲代码当中,使用 St ...
随机推荐
- (转)Unity3D研究院之Assetbundle的实战(六十三)
上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一) 本篇文章我们将说说assetbundl ...
- 关于Promise的一些个人理解jQuery的deferred
一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组), ...
- Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表
本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...
- 谷哥的小弟学前端(10)——JavaScript基础知识(1)
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 具体解释Android主流框架不可或缺的基石 站在源代码的肩膀上全解Scroller工作机制 Android多分辨率适 ...
- SpringBoot添加对Log4j2的支持
1.在添加对Log4j2的支持前,需要先把SpringBoot默认使用的Logback日志框架排除,修改pom.xml文件: <dependency> <groupId>org ...
- 关于Chrome浏览器(Chrome Stable、 Chrome Canary 、Chromium)
作为开发者,web浏览器一般最常用的可能是Chrome浏览器.但其实Chrome浏览器还有别的一些版本.如:Chrome Stable. Chrome Canary .Chromium.大部分人一般用 ...
- robot framework-databaselibaray库使用(python)
公司做项目用到了databaselibaray,刚开始使用时碰到了很多问题,网上也查阅了很多资料终于是可以用了,现在整理记录下来,有需要的同学可随意使用: 另,本文主要是databaselibaray ...
- Kafka 跨集群同步方案(转)
来自:http://tangzhaohui.net/524 Kafka 跨集群同步方案——Kafka内置的MirrorMaker工具 该方案解决Kafka跨集群同步.创建Kafka集群镜像等相关问题, ...
- 来自Unix/Linux的编程启发录
本篇文章已授权微信公众号 guolin_blog (郭霖)独家公布重点内容 2017年第一篇文章,祝各位好友新年快乐. 年前因为不小心坐到了自己左手大拇指导致轻微的骨裂,没有按时更新,实在是羞愧.今年 ...
- javascript库之Mustache库使用说明
一.简单示例 代码: function show(t) { $("#content").html(t); } var view = { title: 'YZF', cacl: fu ...