2010.05.18———struts2 之helloWorld

本文详细介绍了使用Struts2框架实现HelloWorld示例的过程,包括所需jar包、web.xml配置、Action类、JSP页面以及struts.xml配置文件的详细步骤。同时解释了如何使用Struts2的标签库和实现Action接口,提供了从零开始快速搭建Struts2应用的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2010.05.18———struts2 之helloWorld

http://struts.apache.org/
我用的是struts-2.1.8.1-all.zip

1. 所需要的基本jar
[list]
[*]commons-logging-1.0.4.jar
[*]freemarker-2.3.15.jar
[*]ognl-2.7.3.jar
[*]struts2-core-2.1.8.1.jar
[*]xwork-core-2.1.6.jar
[/list]
但是 在struts-2.1.8.1这个版本里面 如果不加
[color=red]commons-fileupload-1.2.1.jar[/color]
这个jar包 会一直报错 很是郁闷


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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


3.action类
在struts2里面可以用普通的java类来作为Action类,只要这个java类中提供了
public String execute();

这个方法就可以了

不过 [color=red]通常我们会去实现com.opensymphony.xwork2.Action这个接口[/color] 它里面定义了execute这个方法,还定义了
5个字符串的静态常量:
[list]
[*]//action执行成功 向用户显示成功页面
[*]public static final String SUCCESS = "success";
[*]//action执行成功,但不需要向用户显示结果页面
[*]public static final String NONE = "none";
[*]//action执行失败,向用户显示失败页面
[*]public static final String ERROR = "error";
[*]//action的执行需要用户输入更多的信息,向用户显示输入页面
[*]public static final String INPUT = "input";
[*]//由于用户没有登录,action不能执行,向用户显示登录页面
[*]public static final String LOGIN = "login";
[/list]

所有的action类 都必须返回一个字符串类型的结果代码,来对应要呈现的result


package action;

import com.opensymphony.xwork2.Action;

public class Action_HelloWorld implements Action{

private String message;
public String getMessage(){
return message;
}

public String execute() throws Exception {
// TODO Auto-generated method stub
message = "hello world";
return SUCCESS;
}

}


注意: 成员变量 要有get方法 ,否则空指针异常


4. jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>helloWorld</title>
</head>

<body>
<h2><s:property value="message"/></h2>
</body>
</html>



struts2的标签库 [color=darkred]<%@ taglib prefix="s" uri="/struts-tags" %>[/color]
不要忘了 uri里面的那个"/"

s:property 输出action对象的message属性


5. struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="helloWorld" class="action.Action_HelloWorld">
<result name="success">/helloWorld.jsp</result>
</action>
</package>
</struts>


package 是把多个action组成一个逻辑单元 name是指定包的名字(自己编的) extends是要扩展的包
可以说是用来继承的

action是对action类进行配置的 name是指定一个名字,即用户访问的url,访问helloWorld.action
class指定action类的完整类名
注意: 1.不要再name的值前面加"/"
2.不要再名字后面加".action"
result是action类和jsp结果页面之间的联系,name是只读action类返回的字符串的值,success即
Action_HelloWorld返回的SUCCESS的值,当然 如果result的名字是“success” 可以省略name属性
/helloWorld.jsp即指定当返回success时 转到的页面


6.访问
部署发布以后 http://localhost:8052/helloWorld.action
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值