一、新建一个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. 使用SQLCOMMAND以及SQLADAPERT 调用存储过程

    使用SQLCommand调用的基本方法如下: SqlCommand comm = new SqlCommand("P_GetCompanyInfo", conn); comm.Co ...

  2. [翻译][Trident] Trident state原理

    原文地址:https://github.com/nathanmarz/storm/wiki/Trident-state ----------------------------- Trident在读写 ...

  3. git 安装与使用场景

    1. 安装 yum install git #自动安装依赖 centos sudo apt-get install git #ubutu http://msysgit.github.io/ #wind ...

  4. angular实践第一弹:选项卡开发

    在学习angular的过程中,实践是最好的方法. 在开发选项卡的过程中,不需要再像jquery一样以DOM操作为核心,那什么样的情况是以DOM操作为核心的Jquery的思想呢? 一想到改变什么,就想设 ...

  5. IE=EmulateIE8和IE=IE8的区别

    IE=8<meta http-equiv="X-UA-Compatible" content="IE=8" />This forces IE 8 t ...

  6. 解决window8 下连接PLSQL 报ora-12154错误

    操作系统版本:window8 64位企业版 数据库:oracle10g2 安装PLSQL,登录PLSQL报ORA-12154错误. 首先:所以需要下载一个32位客户端,我同时也下载了64位客户端,具体 ...

  7. spring含参数 环绕通知demo

    题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么 <?xml version="1.0" encoding="UTF-8" ...

  8. jquery视频展示 图片轮播

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. App Submission Issues

    查看原文: http://leancodingnow.com/app-submission-issues/ I bet many iOS developers are busy submitting ...

  10. 删除重复记录(Mysql,SqlServer,Sqlite)

    Mysql中有重复的数据: ) )> order by count() desc 删除一下吧: delete a from t_resource_apptype_releation as a, ...