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 ...
随机推荐
- NodeJS、NPM安装配置步骤(windows版本)
windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的"INSTA ...
- C++ 模版
函数模版 #include <iostream> using namespace std; template<typename T> T add(T t1, T t2) { r ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- C#将WebBowser控件替换为Chrome内核
摘要 由于最近要做一个浏览器式的软件,其中有不少地方需要使用到jQuery和BootStrap,但是在C#中,默认的WebBrowser控件默认使用的是IE的core,而低版本的IE在JS加载上总是容 ...
- 创建ejs模板的express工程
npm install -g express npm install -g express-generator express -e projectName cd projectName npm in ...
- Android开发之扫描二维码开发
原贴地址:http://www.cnblogs.com/Fndroid/p/5540688.html 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这样的,可以放心 ...
- c#smtp多线程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- XMind共享未保存的思维导图的教程
我们在XMind 6对导图进行局域网共享时,一般都是对XMind文件先进行保存再共享,那样是忘记保存呢,该如何共享.局域网共享功能是XMind 6特有的功能之一,自是较为完善,性能强大的功能,当然有办 ...
- [bzoj1670][Usaco2006 Oct]Building the Moat
Description 为了防止口渴的食蚁兽进入他的农场,$Farmer John$决定在他的农场周围挖一条护城河.农场里一共有$N$股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河 ...
- css-css权威指南学习笔记6
第八章 padding/border/margin 1.对于只包含文本的行,能改变行间距里的属性只有line-height/font-size/vertical-align. 2.对行内非替换元素应用 ...