完整的struts2框架应用实例
一、有关struts的配置,上一篇文章已经介绍过了,这里不再赘述,直接进入主题;
配置网址:(http://blog.csdn.net/linshenshijianlu/article/details/78050209);
二、struts框架所需要的两个配置文件:web.xml和struts.xml
web.xml配置文件主要是配置struts的过滤器,使整个web的流程转入到struts框架中,而struts.xml是struts框架的核心配置文件,在项目开发过程中,需要在此文件中进行大量的配置;
三、了解之后,开始进入实例开发
3.1 建立一个项目,名称为FirstAction
3.2 导入框架所需要的各种jar包(在WEB-INF/lib下,注意一定要确保位置的正确性,否则会因为找不到路径而报错);
3.3 配置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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
<welcome-file-list><welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
3.4 在src下建立一个com.action包,并在包下建立一个java类,注意引用的superclass是com.opensymphony.xwork2.ActionContext;
package com.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction 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{String strReturn=INPUT;if(this.username.equals("abc")&&this.password.equals("123")){strReturn=SUCCESS;}else{ActionContext.getContext().getSession().put("tip","登录失败");}return strReturn;}
}
3.5 再建立两个JSP页面,分别是login.jsp和loginSuccess.jsp(注意这两个页面的放置位置,如果放错,则会出不来结果)
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><center>${tip}<form action="login.action" method="post"><table><tr><td>用户名:</td><td><input type="text" name="username"/></td></tr><tr><td>密码:</td><td><input type="text" name="password"/></td></tr><tr><td colspan="2"><input type="submit" value="登录"/></td></tr></table></form></center>
</body>
</html>
loginSuccess.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>用户登录成功
</body>
</html>
3.6 struts.xml配置文件
(红色内容的引用,参考上面的网址)
<?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" >
<struts><package name="myPackage" namespace="/" extends="struts-default"><action name="login" class="com.action.UserAction" method="execute"><result>loginSuccess.jsp</result><result name="input">login.jsp</result></action></package>
</struts>
3.7结果显示:
目录结构:(注意:一定要确保每一个文件位置的正确性,不然,很容易出错的;)
(lib下的jar文件,使用复制的方式;)
完整的struts2框架应用实例相关推荐
- struts2框架单文件、多文件上传实例详解
版权声明:本文为博主原创文章,如需转载,请标明出处. https://blog.csdn.net/alan_liuyue/article/details/79390681 简介 1.上一篇博客讲解了J ...
- Struts2快速入门实例代码教程
Struts2是基于MVC设计模式的流行和成熟的Web应用程序框架.Struts2并不只是Struts 1下一个版本,但它是一个完全重写的Struts架构. WebWork框架开始了与Struts框架 ...
- Struts2框架的基本使用
前面已经介绍过了MVC思想,Struts2是一个优秀的MVC框架,大大降低了各个层之间的耦合度,具有很好的扩展性.从本篇开始我们学习Struts2的基本用法,本篇主要包括以下内容: Struts2的下 ...
- 使用struts2框架来实现前台与后台的交互
由于这几天到处出差,比较忙,博客的更新速度比较慢.今天的这个是小实例的功能和上一篇博文中的功能是一致的,只是将原来由servlet实现的部分功能用前一段时间比较流行的struts2框架来实现,而现在比 ...
- Struts2框架学习---ONE
通过阅读本你将掌握: struts2框架的配置 入门程序的实现: 配置文件的加载顺序: ...
- SSH:Struts2框架(总结)
摘要 Struts2是在WebWork2基础发展而来的.和struts1一样, Struts2也属于MVC框架.不过有一点大家需要注意的是:尽管Struts2和Struts1在名字上的差别不是很大,但 ...
- java struts2 框架 入门简介
目录 一.Struts2框架执行流程 二.Struts2的快速入门 1.导入jar包 2.配置web.xml文件 3.配置struts.xml文件 4.创建Action来完成逻辑操作 三.Struts ...
- Struts2框架--学习笔记(下):OGNL表达式、值栈操作、拦截器、struts2标签、文件上传
一.OGNL概述:OGNL是一种表达式 (1)在struts2中操作值栈数据. (2)一般把ognl在struts2中操作,和struts2标签一起使用操作值栈. (3)ognl不是strut2的一部 ...
- struts2找不到action_第一次用上Struts2框架做Web开发的体验……
SliiyStruts2 又名 sb-struts2,因为struts真的太难用了(也许在很多年前是很好用的,但是现在看来,被其他框架秒成渣) 前言 想不到我还是得和这struts框架打交道啊,从一开 ...
最新文章
- 重磅消息:Spring 6 和Spring Boot 3
- .如何实现浏览器内多个标签页之间的通信?
- 分区表--SQLServer创建分区表
- Analysis of the Clustering Properties of the Hilbert Space-Filling Curve 论文笔记
- php 验证码文件,php实现的验证码文件类实例
- 摇杆小霸王红白机在线游戏网页源码
- phpMyAdmin登录时指定服务器ip和端口的方法
- php是属于什么软件,.php是什么软件?
- 计算机d盘无法格式化,d盘无法格式化,小编教你解决D盘无法格式化的方法
- 基于遗传算法的柔性车间调度优化(Matlab代码实现)
- linux中匹配邮箱地址,Python正则匹配邮箱和电话号码
- Android 9 隐藏下拉状态栏中的快捷开关
- QlikView 学习资料
- 计算机组成原理ACC MQ,计算机组成原理讲案第一章.ppt
- c++笔记(class)练手项目:暗网杀手排名系统
- markdown笔记(一)—— 首行缩进和换行
- 环形穿梭车(RGV)高效搬运设计方法
- 制作一个网站前你应该了解哪些东西?怎么搭建网站?
- ubuntu10.04下安装fcitx小企鹅输入法,配置,美化及删除多余码表
- android表情符号使用,android评论输入表情符号