一、新建一个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. GC: CMS垃圾回收器一(英文版)

    Memory Management in the Java HotSpot™ Virtual Machine Concurrent Mark-Sweep (CMS) Collector For man ...

  2. ]用EnumChildWindows遍历窗口的方法

    最近项目有需要,得到一个非自己实现的窗口控件对象.于是想起曾经做过类似功能.总结如下: 调用EnumChildWindows(this->m_hWnd, EnumChildProc, NULL) ...

  3. (博弈论)hdoj 1079 Calendar Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题解:题目大意,两个人Adam和Eve一块儿玩游戏,游戏规则是从1900年1月1日到2001年1 ...

  4. java使用jdbc对sqlite 添加、删除、修改的操作

    package com.jb.jubmis.Dao.DaoImpl; import java.io.File;import java.io.FileInputStream;import java.io ...

  5. Plan04.学习与提升

    虽然工作没有继续做自己最喜欢的Android的开发,对于自己来说,从事J2EE又是一种挑战,自己 可以学习更多的东西,开阔自己的眼界,而不是局限在Android的应用开发领域. 工作这段时间,自己学到 ...

  6. MRI中T1和T2的含义与区分[转]

    A. MRI名词解释   T1加权像.T2加权像为磁共振检查中报告中常提到的术语,很多非专业人士不明白是什么意思,要想认识何为T1加权像.T2加权像,请先了解几个基本概念:   1.磁共振(maget ...

  7. 你真的会玩SQL吗?实用函数方汇总

    http://www.cnblogs.com/zhangs1986/p/4917800.html 实用函数方法 由于有些知识很少被用到,但真需要用时却忘记了又焦头烂额的到处找. 现在将这些‘冷门“却有 ...

  8. 详解MyEclipse10 安装Spket 1.6.23(支持Extjs4.1.1及jQuery1.8)

    用MyEclipse10安装Spket主要有3种方式:在线下载更新.下载Zip覆盖.下载jar包安装.我用在线安装尝试了N次终于还是失败,只好下载jar包来安装,在失败了M次之后终于安装成功,现在网上 ...

  9. DBCP连接池介绍

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...

  10. android如何实现开机自动启动Service或app

    第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app. import and ...