struts2 零配置
一、新建一个web项目,命名为:struts2

二、导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3

三、配置struts2的启动文件,在web.xml添加如下内容
<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>
四、在src包下,新建struts.xml和struts.properties这2个文件

在struts.xml添加如下内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="true"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 含有Action类的路 从action包开始-->
<constant name="struts.convention.package.locators" value="action" />
<package name="json" extends="json-default"> </package> </struts>
在struts.properties文件添加如下内容(指定结果页面的路径)
struts.convention.result.path=/
五、现在我们已经完成了strut2的环境配置了,接下来说介绍个使用的demo
demo1
在src下新建一个包命名为:com,在com包下新建一个包命名为:action,在action包下新建一个包命名为:demo

在demo 包下新建一个IndexAction.java的类
package com.action.demo; import org.apache.struts2.convention.annotation.ParentPackage;
import com.opensymphony.xwork2.ActionSupport; @ParentPackage(value = "struts-default")
public class IndexAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -903752277625921821L;
private String name; @Override
public String execute() {
setName("stuts2 零配置的实现");
return SUCCESS;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
在WebRoot目录下新建一个文件夹命名为:demo

在WebRoot/demo目录新建一个index.jsp 内容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
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 'index.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>
<s:property value="name"/>
</body>
</html>
启动tomcat,在浏览器输入:http://localhost:8080/struts2/demo/index

demo2
在src/com/action/demo包下新建一个类IndexTestAction.java
package com.action.demo; import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport; @Action
public class IndexTestAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -903752277625921821L;
private String name; @Override
public String execute() {
setName("stuts2 零配置的实现,路径配置," +
"IndexTestAction对应的结果界面:WebRoot/demo/index-text.jsp action去掉," +
"中间有大写的转换成小写,加上'-' " +
"例如:TestDemoAction 结果页面的是:test-demo.jsp");
return SUCCESS;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
在WebRoot/demo目录新建一个index-test.jsp 内容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
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 'index.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>
<s:property value="name"/>
</body>
</html>
启动tomcat,在浏览器输入:http://localhost:8080/struts2/demo/index-test
struts2 零配置的更多相关文章
- Struts2 Convention Plugin ( struts2 零配置 )
Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...
- 菜鸟学Struts2——零配置(Convention )
又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...
- Struts2零配置介绍(约定访问)
从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...
- spring+hibernate+struts2零配置整合
说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...
- 注解实现struts2零配置
零配置指的是不经过配置文件struts.xml配置Action 首先:导入jar struts2-convention-plugin-2.3.24.1.jar package com.action ...
- struts2零配置參考演示样例
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...
- 13、零配置Struts2开发
Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...
- Struts2的零配置和rest插件
1. 零配置使用struts2-convention-plugin-2.3.16.jar,rest使用struts2-rest-plugin-2.3.16.jar 1.1 Struts2的conven ...
- 从struts2.1开始Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...
随机推荐
- Gym 101064 D Black Hills golden jewels (二分)
题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...
- Codeforces 707 E. Garlands (二维树状数组)
题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...
- [Mac]Mac中显示资源库文件夹
在 Mac OS X 10.7 Lion 之后的版本中 , 用户的个人目录内的资源库文件默认是隐藏状态. 这个设定可能是为了避免用户误操作. 但是对于中高级用户来说会有些不变. 通过如下方式可以找回被 ...
- 前端MVC学习总结(三)——AngularJS服务、路由、内置API、jQueryLite
一.服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可以被视为重复使用的执行一个或多个相关任务的代码块. AngularJS服务是单例对象, ...
- erlang: Programming Rules and Conventions。
http://www.erlang.se/doc/programming_rules.shtml#HDR33 http://www.erlang.org/eeps/eep-0008.html
- wikioi 1098 均分纸牌
题目描述 Description 有 N 堆纸牌,编号分别为 1,2,-, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...
- 纯CSS打造Flow-Steps导航
几个要点: 1.三角箭头效果是用border实现的,详细的可以google下CSS 三角 2.IE6下不支持border-color:transparent(透明),解决方法是先将其设置为一个不常用的 ...
- hotfix分析
使用System Update Readiness Tool for Windows Server 2008 R2 x64 可以分析hotfix是否有安装失败的情况 示例:http://blogs.t ...
- Windows Server Backup备份Exchange2010
在Windows Server 2008 R2 SP1上Exchange2010 DAG备份测试成功: 1.分别在DAG成员服务器上安装WSB,不可以安装其命令行工具,因为其需要早期的PowerShe ...
- 【M30】代理类
1.考虑二维数组,在栈上分配,必须在编译时确定大小,也就是大小是常量.另外一点,C++不支持在堆上分配二维数组.怎么解决这个问题? 二维数组可以看成,一维数组的数组.因此,可以使用代理类,Array2 ...