Struts2入门示例
1、导入struts2需要的9个包到lib文件夹中

2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
如上面所示:配置了StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter就会自动加载struts2框架。配置好了filter后,还需要配置filter拦截的URL。一般让拦截用户所有请求。配置filter还可以是org.apache.struts2.dispatcher.FilterDispatcher,但是该filter已近过时了,所有还是用StrutsPrepareAndExecuteFilter
3、Action控制器
在src/action文件夹下面创建一个ShowWords类
package action;
public class ShowWords {
private String name;
private String words;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public String execute(){
if(name.equals("")){
return "input";
}else{
words="欢迎您:"+name;
return "success";
}
}
}
如上ShowWords类,添加了name和words属性,并在action中执行默认方法execute,判断如果字符串为空,则返回逻辑视图input,否则返回success逻辑视图
4、配置struts.xml
在src下面新建一个struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.action.extension" value="action,do,"></constant>
<package extends="struts-default" name="default" namespace="/">
<action name="showWords" class="action.ShowWords">
<result>/Hello.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
其中dtd名称空间可以拷贝struts2-core-2.3.28.1.jar包下面struts-default.xml下面的DOCTYPE
constant:用于配置struts默认配置,例如struts.i18n.encoding,用于设置字符编码;struts.action.extension:用于设置url的扩展名,这里设置可以不需要扩展名,也可以用action或do作为扩展名
struts框架默认属性保存在struts2-core-2.3.28.1.jar/org.apache.struts2/default.properties中
extends="struts-default":表示该配置继承自srtuts-default,而struts-default配置在struts-default.xml文件中
<action name="showWords" class="action.ShowWords">
name:url访问路径 class:action类
在action标签中还有一个method属性,该属性用来指定需要执行action类中的什么方法,如果不指定,默认方法execute方法
<result>/Hello.jsp</result>
<result name="input">/index.jsp</result>
根据action类返回结果,来控制视图
name属性即为action的返回值,如果不设置name默认为success,而result中文本表示需要执行的视图jsp的地址
Struts2入门示例的更多相关文章
- Struts2入门示例(Myeclipse)
1.新建Web项目在lib导入struts-2.3.37核心基础jar包 2.在WebRoot新建2个JSP demo1.jsp <%@ page language="java&quo ...
- [转] Struts2入门示例教程
原文地址:http://blog.csdn.net/wwwgeyang777/article/details/19078545/ 回顾Struts2的使用过程,网上搜的教程多多少少都会有点问题,重新记 ...
- struts2入门示例(hello world)
1. 环境搭建 按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了.首先去struts的官网下载好最新的struts2代码 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- struts2入门程序
struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...
- [WCF编程]1.WCF入门示例
一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...
- Struts2 入门
一.Struts2入门案例 ①引入jar包 ②在src下创建struts.xml配置文件 <?xml version="1.0" encoding="UTF-8&q ...
- Maven入门示例(3):自动部署至外部Tomcat
Maven入门示例(3):自动部署至外部Tomcat 博客分类: maven 2012原创 Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...
- 1.【转】spring MVC入门示例(hello world demo)
1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...
随机推荐
- 【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
2288: [POJ Challenge]生日礼物 Description ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超 ...
- 【找规律】【递归】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem F. Doubling
题意: 给你一个n,问你R(n)对应的字符串长度最小的是啥. dp打个表出来,f(i)表示i值对应的字符串的最小长度,发现f(1)=1,f(2)=2,其他的情况下,若是偶数,则恰好在其外面加一对中括号 ...
- 【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵
hdu1166 敌兵布阵,单点修改,区间求和. [ATTENTION]MAXN要开成节点数的4倍,开得不够会提示TLE. #include<iostream> #include<cs ...
- Android中Acition和Category常量表
Action Action常量 对应字符串 简单说明 ACTION_MAIN android.intent.action.MAIN 应用程序入口 ACTION_VIEW android.intent. ...
- JDK源码学习笔记——String
1.学习jdk源码,从以下几个方面入手: 类定义(继承,实现接口等) 全局变量 方法 内部类 2.hashCode private int hash; public int hashCode() { ...
- 都9102年了,还不会Docker?10分钟带你从入门操作到实战上手
Docker简述 Docker是一种OS虚拟化技术,是一个开源的应用容器引擎.它可以让开发者将应用打包到一个可移植的容器中,并且该容器可以运行在几乎所有linux系统中(Windows10目前也原生支 ...
- codevs 1080 线段树练习--用树状数组做的
1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现在动态 ...
- Xcode 6.4项目中的常见文件(info.plist)
Xcode 6.4项目中的常见文件(info.plist) 代码中获取 info.plist[NSBundle mainBundle] infoDictionary]; Bundle display ...
- WPF中的动画——(六)演示图板
前面所介绍的都是单一的动画,它只能修改单一属性.有的时候,我们需要将一组动画一起进行,对于一个按钮,我们可能有如下需求: 选择该按钮时,该按钮增大并更改颜色. 单击该按钮时,该按钮缩小并恢复其原始大小 ...
- UITabBarControlller 和 UINavigationController