1. 环境搭建

按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了。首先去struts的官网下载好最新的struts2代码,然后解压,可得到一个名为struts-x.x.x的文件夹。进入apps,解压struts2-blank.war(若不能直接解压,可修改后缀名后解压),得到文件夹struts2-blank。进入struts-x.x.x\apps\struts2-blank\WEB-INF\lib,拷贝所有jar文件,复制到web项目下的webRoot->WEB-INF->lib下。然后拷贝struts-x.x.x\apps\struts2-blank\WEB-INF\web.xml,复制到web项目下的webRoot->WEB-INF。最后拷贝struts-x.x.x\apps\struts2-blank\WEB-INF\classes\struts.xml,复制到web项目下的src文件夹中。

2. struts.xml代码

structs.xml代码复制到项目中,请注释掉<struts>到</struts>之间的所有内容。修改后的代码如下:

<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />   <!-- package的name可以随便取 -->
<package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>     <!-- action的name与jsp中的提交处指定的action名应相同。比如:在jsp页面有个form,则form提交后的action应与struts.xml中的某action名相同 -->
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package> <include file="example.xml"/>
-->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true"/> <package name = "hello" namespace = "/hello" extends = "struts-default">
<action name = "hello" class = "com.sun.struts.HelloWorld">
<result>/hello_world.jsp</result>
</action>
</package> </struts>

3. Action代码

我们还需要action来为我们返回一个代表成功的值,从而展现正确的结果页面。右键src,选择new个class,为package起名为com.sun.struts,为class起名为HelloWorld.java。类代码如下:

package com.sun.struts;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
public String execute(){
return SUCCESS;
}
}

4. result代码

当action执行成功后,就会按照struts中所描述的,显示/hello_world.jsp页面了。页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
hello world! <br>
</body>
</html>

 5. 访问

完成如上步骤后,就应该可以成功访问url: http://localhost:8080/web项目名/hello/hello 了,页面应该显示:hello world!

struts2入门示例(hello world)的更多相关文章

  1. Struts2入门示例(Myeclipse)

    1.新建Web项目在lib导入struts-2.3.37核心基础jar包 2.在WebRoot新建2个JSP demo1.jsp <%@ page language="java&quo ...

  2. [转] Struts2入门示例教程

    原文地址:http://blog.csdn.net/wwwgeyang777/article/details/19078545/ 回顾Struts2的使用过程,网上搜的教程多多少少都会有点问题,重新记 ...

  3. Struts2入门示例

    1.导入struts2需要的9个包到lib文件夹中 2.配置web.xml文件 <?xml version="1.0" encoding="UTF-8"? ...

  4. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  5. struts2入门程序

    struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...

  6. [WCF编程]1.WCF入门示例

    一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...

  7. Struts2 入门

    一.Struts2入门案例 ①引入jar包 ②在src下创建struts.xml配置文件 <?xml version="1.0" encoding="UTF-8&q ...

  8. Maven入门示例(3):自动部署至外部Tomcat

    Maven入门示例(3):自动部署至外部Tomcat 博客分类:  maven 2012原创   Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...

  9. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

随机推荐

  1. selenium TestNG基本注释和属性

    TestNG注释详解 suite 属性说明: @name: suite 的名称,必须参数@junit:是否以Junit 模式运行,可选值(true | false),默认"false&quo ...

  2. Appium +Python 连接真机测试

    1.数据线连接电脑和手机: 2.用adb获取手机的UUID:cmd-> adb devices 前面的就是你手机的UUID 3.打开appium,选择手机的安卓版本(关于手机中查看),填写手机的 ...

  3. Codeforces 811 A. Vladik and Courtesy

    A. Vladik and Courtesy   time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. 前端学习blog

    有一些js写的小游戏,很有趣  http://oldj.net

  5. cogs p服务点设置

    5. P服务点设置 ★★   输入文件:djsc.in   输出文件:djsc.out   简单对比时间限制:2 s   内存限制:128 MB 问题描述为了进一步普及九年义务教育,政府要在某乡镇建立 ...

  6. Oracle doesn't have on duplicate key update Use MERGE instead:

    Oracle doesn't have on duplicate key update Use MERGE instead: MERGE INTO my_table trg USING (SELECT ...

  7. 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers

    线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...

  8. java web(学习笔记)项目路径问题

    最近刚接触java web特别是是关于项目路径这一块很晕,就把自己遇到的一些疑惑和理解写下来. 首先贴上路径,这里用的是eclipse. 其中我们要注意看WebContent目录,这是web程序的根目 ...

  9. [读书笔记] 你早该这么玩Excel

    <你早该这么玩Excel>只教你做两件事:如何设计一张“天下第一表”,你会恍然大悟,以前遇到的种种麻烦是因为做错了表格:如何一分钟“变”出N张表,你会明白表格是“变”出来的,不是“做”出来 ...

  10. 移植u-boot-2012.4到Tiny6410_1G_Nandflash

    Uboot-2012.4的启动流程: 自从Uboot-2010后的Uboot在文件结构和启动流程方面都有非常大的改变,所以移植Uboot-2012.4的时候还是费了我不少时间, 首先在编译Uboot的 ...