一、新建一个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 零配置的更多相关文章

  1. Struts2 Convention Plugin ( struts2 零配置 )

    Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...

  2. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  3. Struts2零配置介绍(约定访问)

    从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...

  4. spring+hibernate+struts2零配置整合

    说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...

  5. 注解实现struts2零配置

    零配置指的是不经过配置文件struts.xml配置Action 首先:导入jar   struts2-convention-plugin-2.3.24.1.jar package com.action ...

  6. struts2零配置參考演示样例

    <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2 ...

  7. 13、零配置Struts2开发

    Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...

  8. Struts2的零配置和rest插件

    1. 零配置使用struts2-convention-plugin-2.3.16.jar,rest使用struts2-rest-plugin-2.3.16.jar 1.1 Struts2的conven ...

  9. 从struts2.1开始Convention零配置

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...

随机推荐

  1. DMOZ介绍以及如何提交

    转载自 http://www.cnblogs.com/freespider/archive/2009/12/28/1633818.html Dmoz介绍及怎么提交? 1.Dmoz目录简介: Dmoz是 ...

  2. servlet-3_0-final-spec

    <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://w ...

  3. 浅谈iOS IPv6-only 新规

    5月份苹果发布新规,对于开发人员只需要做到以下几点就能顺利上线啦! 1.苹果从6月1日起,提供App Store审核的应用必须要兼容面向硬件识别和网络路由的最新互联网协议--IPv6-only标准.也 ...

  4. poj 3134 Power Calculus(IDA*)

    题目大意: 用最小的步数算出  x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉 #include < ...

  5. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  6. 从零新建一个winform项目

    网站:https://community.devexpress.com/blogs/eaf/archive/2012/10/30/xaf-application-from-scratch.aspx

  7. 组合View Controller时遇到的一点问题

    View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation v ...

  8. Unable to execute dex: Multiple dex files define 解决方法

    程序编译正常,在用Eclipse调试执行时,报错Unable to execute dex: Multiple dex files define: 方法:      原因是有重复的.jar被引用,可以 ...

  9. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  10. Codeforces Beta Round #18 (Div. 2 Only) C. Stripe 前缀和

    C. Stripe Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/18/C ...