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插件更彻底,该 ...
随机推荐
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- CodeForces 710B Optimal Point on a Line (数学,求中位数)
题意:给定n个坐标,问你所有点离哪个近距离和最短. 析:中位数啊,很明显. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240000 ...
- PL/SQL devloper 常用设置
1)代码自动完成 Tools->Preferences->User Interface->Key Configuration. 找到Tools/Code Assistant,修改为自 ...
- C#调用存储过程详解
连接字符串: string conn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].C ...
- 网络虚拟化技术 TUN/TAP MACVLAN MACVTAP
TUN 设备 TUN 设备是一种虚拟网络设备,通过此设备,程序可以方便得模拟网络行为.先来看看物理设备是如何工作的:
- jquery 禁止页面滚动-移动端
禁止 window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.r ...
- jquery获取当前元素的坐标
jquery获取当前元素的坐标 1,获取对象 var obj = $("#id号"); 或 var obj = $(this); 实例中我获取的对象是弹出窗口按钮,这样创建的新窗 ...
- Eclipse配置PyDev插件
安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...
- MySQL 5.7.13解压版安装记录 mysql无法启动教程
1 解压缩 2 添加环境变量,这个不细说了 我的电脑->属性->高级->环境变量 选择PATH,在其后面添加: 你的mysql bin文件夹的路径 (如:C:\Program Fil ...
- Shared File System Master Slave 全配置以及测试
在本机完成2个broker的共享文件测试 2个broker的完整配置文件如下 <!-- Licensed to the Apache Software Foundation (ASF) ...