搭建一个简单的Struts2框架
1 创建一个web项目。
2 导入必要的JAR文件。
放在WEB-INF下lib包里。
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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsDemo</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>
<!-- struts2.1.3之后的版本,可以在该过滤器之前之间定义一定的过滤器-->
<!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求-->
<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>
4 编写Action。
- Struts2直接使用Action来封装HTTP请求参数,因此Action类应该包含与请求相对应的属性,并为该属性提供对应的setter和getter方法。
- 为Action类里增加一个execute方法,因为Struts2框架默认会执行这个方法。这个方法本身并不做业务逻辑处理,而是调用其他业务逻辑组件完成这部分工作。
- Action类返回一个标准的字符串,该字符串是一个逻辑视图名,该视图名对应实际的物理视图。
我们来写个用户登录验证,提供用户名和密码两个属性。如果正确返回success否则返回error。
package com.cy.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String userName;
private String password; public String execute() { if (userName.equals("hellokitty") && password.equals("123")) { return SUCCESS;
} else {
return ERROR;
} } 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;
} }
Action有一下特点:
- Struts2框架中Action是一个POJO,没有被代码污染。
- Struts2中的Action的execute方法不依赖于servlet API,改善了Struts1中耦合过于紧密,极大方便了单元测试。
- Struts2的Action无须用ActionForm封装请求参数。
5 添加框架核心配置文件struts.xml文件:在WEB-INF/classes文件夹下创建struts.xml。
在struts2-core-2.3.16.jar中有strust-defalut.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>
<package name="default" extends="struts-default">
<action name="login" class="com.cy.action.LoginAction">
<result name="success">/jsp/success.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</package> </struts>
- 在action标签中定义了name和class。name属性对应的是用户URL请求中的action名,class属性是处理请求的实现类(注意:要包含完整路径)。
- result标签定义逻辑视图和物理视图之间的映射,在我们的Action中,如果返回的字符串是"success”则由对应的success.jsp页面进行处理;如果返回的字符串是"error”则由error.jsp页面进行处理。
6 编写界面
6.1 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>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="login" method="post">
用户名:<input type="text" name="userName"><br/>
密 码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
6.2 success.jsp
<body>
<h1>登录成功!</h1>
</body>
6.3 error.jsp
<body>
<h1>登录失败!</h1>
</body>
整体:
搭建一个简单的Struts2框架的更多相关文章
- 搭建一个简单的springMVC框架
//新建一个简单的maven项目,选择war包 //web.xml配置 <?xml version="1.0" encoding="UTF-8"?> ...
- 搭建一个简单的mybatis框架
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- Flutter学习三之搭建一个简单的项目框架
上一篇文章介绍了Dart的语法的基本使用,从这篇文章开始,开发一个基于玩Android网站的app.使用的他们开放的api来获取网站数据. 根据网站的结构,我们app最外层框架需要添加一个底部导航栏, ...
- 搭建一个简单的Struts2(Struts2_HelloWorld)
1.导入Jar包 2.配置web.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-ap ...
- 使用maven+eclipse搭建最简单的struts2的helloworld
使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 如何创建一个简单的struts2程序
如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...
- 【netty】(2)---搭建一个简单服务器
netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...
- Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单
原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...
随机推荐
- CSS选择器及其优先级
一:一些普通的选择器 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...
- grails-shiro权限认证
一.引用shiro插件 //在BuildConfig的plugins下面添加 compile ":shiro:1.2.1" 二.引用新插件后要进行编译 //grails命令 com ...
- ASP.NET导入导出
//创建一个数据链接 // string strCon =" Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Extended Prope ...
- BZOJ 3434 时空穿梭
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3434 题意: 思路: const int mod=10007; const int N=1 ...
- BZOJ 2324 营救皮卡丘(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2324 题意:n+1个城市(0到n).初始时K个 人都在0城市.城市之间有距离.要求(1) ...
- 关于mysqli_fetch_assoc的一点说明
关于mysqli_fetch_assoc的一点说明 如下2种用法是错误的: 错误1 .... $fetchResult = mysqli_fetch_assoc($queryResult); ...
- FreeSWITCH 1.6在Debian 8上的安装
鉴于上次在CentOS 7上安装不成功,这次换Debian. 现在已经成功的CentOS 7上安装好了. 感兴趣的同学移步https://freeswitch.org/confluence/displ ...
- Task<TResult>的使用
https://msdn.microsoft.com/en-us/library/dd321424(v=vs.110).aspx Represents an asynchronous operatio ...
- Calling / Running a report in Oracle forms 10g / 11g
Calling / Running a report in Oracle forms 10g / 11g Below is the procedure to call a report in Orac ...
- [转]Jenkins CommonCollections 完美利用(演示)工具
博主URL:http://tools.changesec.com/Jenkins-CommonCollections-Exploit/ 提交漏洞总是要证明漏洞危害,老外写的java代码又有bug,所以 ...