Struts2第一个入门案例
一.如何获取Struts2,以及Struts2资源包的目录结构的了解
Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache Struts菜单下的Release链接,可以查看Struts各个阶段的词资源,也可以通过Archive Site链接访问来获取版本。
那我们这里以struts-2.3.15.1-all为例。

1.App目录下包含了官方提供的Struts2应用示例,为开发者提供了很好的参照。
2.doc目录下是官方提供的Struts2文档。
3.lib目录下是Struts的发行包及其依赖包。
4.src目录是Struts2项目该版本对应的源码。
其余部分是Struts2及其依赖包的使用许可协议和声明。
二.入门案例
1.引入jar包:
新建一个Java Web项目,将Struts2框架所需的jar包添加到项目的lib文件夹上,Struts2项目所依赖的基础jar包如下:

2.创建一个登录案例的界面
01.login.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- 导入Struts2 核心标签库-->
<%@taglib uri="/struts-tags" prefix="s"%>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.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">
</head>
<body>
<form action="Login3.action" method="post">
用户名:<input type="text" name="user.name" /> 密码:<input type="text"
name="user.pwd" /> <input type="submit" value="登录">
</form>
</body>
</html>
02.登录失败页面:final.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
<title>My JSP 'final.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">
</head>
<body>
<h1>登录失败</h1>
</body>
</html>
03.登录成功页面scuess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ taglib uri="/struts-tags" prefix="s"%>
<head>
<base href="<%=basePath%>">
<title>My JSP 'scuess.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">
</head>
<body>
<h1>登录成功! 欢迎你,<s:property value="user.name"/> </h1>
</body>
</html>
3.创建cn.entity.User类:
package cn.entity;
//用户类
public class User {
private String name;
private String pwd;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
4.创建cn.action.LoginAction类:
package cn.acction;
import cn.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LojinAction implements Action {
// 注意User对应必须要有get和set封装不然Strut2框架不会帮你自动装备User对象
private User user;
@Override
public String execute() throws Exception {
// 如果密码为123,用户名为123表示登录成功
if (user.getName().equals("123") && user.getPwd().equals("123")) {
// 表示登录成功。
return SUCCESS;
} else {
// 表示登录失败
return ERROR;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
5.配置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>
<!-- 添加struct2的核心过滤器 -->
<filter>
<filter-name>struct</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struct</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
6.在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>
<!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat -->
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 登录案例 -->
<action name="Login" class="cn.acction.LojinAction">
<result name="scuess">
Login/scuess.jsp
</result>
<result name="error">
Login/final.jsp
</result>
</action>
</package>
</struts>
执行流程图:

注意点:
1.一定要在web.xml中配置struts2核心过滤器。
2.from表单的action提交的名字要和struts.xml中的action节点的名字保存一致。
3.struts.xml的名字不要写错。
4.表单的input的name属性值不要写错,要写成执行Action类中成员变量的属性名。
Struts2第一个入门案例的更多相关文章
- struts2第一天——入门和基本操作
一.概述 1.运用场景: 应用于三层架构中web层的框架(显示层的运用),是经典MVC模型的web应用的变体. 2.与struts1的对比: struts2是在struts1基于webwork发展的全 ...
- Struts2 第一个入门小案例
1.加载类库 2 配置web.xml文件 3.开发视图层 4.开发控制层Action 5.配置struts.xml 6.部署运行
- SpringMVC的第一个入门案例
用户提交一个请求,服务器端处理器在接收到这个请求后,给出一条欢迎信息,在页面中显示. 第一步:导入jar包 在原有Springjar包基础上添加2个jar包 spring-webmvc-4.2.0.R ...
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...
- 一、mybatis入门案例
今天学习了mybatis框架,简单记录一下mybatis第一个入门案例,目标是使用Mybatis作为持久层框架,执行查询数据的SQL语句并且获取结果集 基本步骤: 物理建模 逻辑建模 引入依赖 创建持 ...
- Struts2入门案例
struts2最简便的案例 Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点.Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架 ...
- Struts2第一天
Struts2第一天 整体课程安排:3天知识点+2天练习 第一天:入门(action和result结果集)--一般的请求+响应 第二天:请求数据处理相关(参数接收.类型转换.合法性校验.国际化) 第三 ...
- Spring第一天——入门与IOC
大致内容 spring基本概念 IOC入门 [17.6.9更新],如何学习spring? 掌握用法 深入理解 不断实践 反复总结 再次深入理解与实践 一.Spring相关概念 1.概述: Sprin ...
- SpringMvc核心流程以及入门案例的搭建
1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...
随机推荐
- [LeetCode] Heaters 加热器
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- python学习之路 第二天
1.import 导入模块 #!/usr/bin/python # -*- coding:utf-8 -*- import sys print(sys.argv) 2.字符串常用方法: 移除空白: s ...
- swfit-学习笔记(表UITableView的简单使用)
/*使用与Object-C基本类似,只做简单地使用,创建表及其设置数据源和代理*/ import UIKit class ViewController: UIViewController,UITabl ...
- iOS App上架流程(2016详细版)
iOS App上架流程(2016详细版) 原文地址:http://www.jianshu.com/p/b1b77d804254 感谢大神整理的这么详细 一.前言: 作为一名iOSer,把开发出来的Ap ...
- C#设计模式(2)——简单工厂模式
一.概念:简单工厂模式(Simple Factory Pattern)属于类的创新型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创 ...