Struts2之HelloWorld
首先既然是开发Struts程序的话,那么自然需要用到Struts2开发包,Struts2是apache旗下的开源框架,所有的开发包和源代码都可以在Apache官网下载。
那么,就来开始编写第一个Struts2程序。

1、新建一个Dynamic web project。 把必须的java包拷贝到lib目录下。
至此准备工作就完成了,下面开始编写第一个Hello World!!!
2、首先,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>
3、编写Action,要想实现Struts2程序有三种方法
①实现Action接口(com.opensymphony.xwork2.Action)
②继承ActionSupport类 (com.opensymphony.xwork2.ActionSupport)
③继承一个自定义的类
由于ActionSupport类已经实现了Action接口,而且提供了更加丰富的操作方法,所以一般采用继承ActionSupport类的方式。
1 package com.fuwh.struts2;
2
3 import com.opensymphony.xwork2.Action;
4
5 public class HelloWorld implements Action{
6
7 private String HelloWorld;
8 public String getHelloWorld() {
9 return HelloWorld;
}
public void setHelloWorld(String helloWorld) {
HelloWorld = helloWorld;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
HelloWorld="世界你好!!!";
return SUCCESS;
}
}
4、Action编写好了之后,需要编写一个struts.xml的配置文件,来管理Action的跳转关系
<?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>
<package name="helloWorld" extends="struts-default">
<action name="hello" class="com.fuwh.struts2.HelloWorld">
<result name="success">HelloWorld.jsp</result>
</action>
</package>
</struts>
5、最后来编写HelloWorld.jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
${HelloWorld}
</body>
</html>
6、将项目发布到服务器,请求如下连接,就大功告成了
http://localhost/Struts2/hello
7,最后来谈谈Struts2的工作原理

Struts2之HelloWorld的更多相关文章
- 使用maven+eclipse搭建最简单的struts2的helloworld
使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...
- 创建一个struts2的HelloWorld
1.下载struts2的jar包 http://struts.apache.org/download.cgi#struts255 下载一个稳定版本Struts 2.3.31 里面提供了maven ja ...
- Struts2 的 helloworld
配置步骤: 1.在你的strut2目录下找到例子项目,把它的 lib 下的jar拷贝到你的项目.例如我的:struts-2.3.24\apps\struts2-blank 2.struts-2.3.2 ...
- 使用MyEclipse 9.0 创建 struts2 的HelloWorld 工程
作为眼高手低的典范,必须痛改前非,好好动手多做开发! 本文是struts2的入门指导,权当笔记! 在MyEclipse中,配置Apache tomcat 7.x 服务器 创建新的Web project ...
- 2.第一个Struts2程序-HelloWorld程序
1.新建Web Project项目:Study_Struts2 2.新建HelloWordAction.java类 3.复制struts.xml文件到src目录下,配置struts.xml文件内容如下 ...
- struts2的HelloWorld的基本过程
login.jsp中 <form action="Login"... 该页面提交后, web.xml中设置的过滤器会起作用 <filter> ...
- Struts2学习笔记 - HelloWorld总结
相信网上已经有很多关于struts2的HelloWorld可参考,我这里就不重复了,没个学struts2的人都会做过很多个HelloWorld,而我这里就总结一下一个简单的HelloWorld 我在一 ...
- 菜鸟学Struts2——Struts工作原理
在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...
- 2. Struts2 基础
1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...
随机推荐
- win7 64位下vs不能以管理员身份运行的问题解决
开发机上安装了VS6.0/2008/2010/2013,之前一直是正常的,突然莫名其妙不能以管理员身份运行(除了VS6),报"application cannot start.", ...
- JavaScript 事件
事件 概念:事件是可以被 JavaScript 侦测到的行为. JavaScript 使我们有能力创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触 ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- phpStudy2016 配置多个域名期间遇到的问题
第一步 在C:\Windows\System32\drivers\etc下的hosts文件下添加 第二步 找到Apache 下的httpd.conf 文件 打开,去掉171行前边的# 第 ...
- iOS关于通知传值Bool类型的注意点
比如: [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_Name" object ...
- 6-2 sed 命令
1. sed : stream editor,流编辑器 是处理纯ASICC纯文本,按行琢行操作的. 编辑器有两种,行编辑器和全屏编辑器 sed:默认不编辑原文件,仅对模式空间中的数据做处理,而后.处理 ...
- 1-1 Linux系统安装
虚拟机下配置网络时 rhel7.2安装新建虚拟机内存2G CPU 1核2线 硬盘20G存为单个文件 使用ISO镜像 桥接网卡引导界面: Install Red Hat Enterprise L ...
- SpringMVC(四) RequestMapping请求方式
常见的Rest API的Get和POST的测试参考代码如下,其中web.xml和Springmvc的配置文件参考HelloWorld测试代码中的配置. 控制类的代码如下: package com.ti ...
- GO语言总结(3)——数组和切片
上篇博文简单介绍了一下Go语言的基本类型——GO语言总结(2)——基本类型,本篇博文开始介绍Go语言的数组和切片. 一.数组 与其他大多数语言类似,Go语言的数组也是一个元素类型相同的定长的序列. ( ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...