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 官网 ...
随机推荐
- Percona TokuDB
Percona TokuDB Percona TokuDB 1. TokuDB说明 2. TokuDB安装 3. 使用TokuDB 3.1 快速插入和富索引 3.2 聚集sec ...
- CSS3:backgroud-size和background-origin的使用方法
backgroud-size CSS3以前,背景图像大小由图像的实际大小决定,background-size指定背景图像的大小. 你可以指定像素或百分比大小,你指定的大小是相对于父元素的宽度和高度的百 ...
- C++实例讲解Binder通信
binder是android里面的通信机制,这就不说它如何如何好了,Goog已经说过了,这里不多说.binder是一个面向对象的编程方法,大量使用虚函数类.最近研究binder看到一网友写的,就借鉴一 ...
- ubuntu kylin 14.04安装搜狗输入法
1.卸载原有的输入法,fcitx或ibus.如卸载fcitx: sudo apt-get remove fcitx*(如不需保留配置文件用purge) sudo apt-get autoremove( ...
- OC中加载html5调用html方法和修改HTML5内容
1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webViewDidFinishLoad: 4.在代理方法中进行操作H ...
- 初始webservice
webservice 可以用来查天气,以及手机号码类型等功能,这写都是简单的 方法有很多: 1.通过创建 web service exploer 创建出一个web services explorer ...
- 00 Cadence学习总目录
这个系列是我学习于博士CADENCE视频教程60讲时,一边学一边记的笔记.使用的CADENCE16.6. 01-03课 了解软件 创建工程 创建元件库 分裂元件的制作方法 04课 正确使用hetero ...
- 有关日期的函数操作用法总结,to_date(),trunc(),add_months();
相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- codevs 1164 统计数字
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description [问题描述]某次科研调查时得到了n个自然数,每个数均不超过150000000 ...