Struts2--HelloWorld

因为struts2是通过filter启动的,而web.xml配置文件中的StrutsPrepareAndExecuteFilter的init()方法会读取类路径下默认的配置文件struts.xml完成初始化操作,故我们需要首先配置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>
	<package name="struts" namespace="/test" extends="struts-default">
		<action name="helloworld" class="cn.struts.demo.HelloWorldAction"
			method="execute">
			<result name="success">/WEB-INF/page/hello.jsp</result>
		</action>
	</package>
</struts>

 

下面对struts.xml配置文件进行说明:

1、struts2框架中使用包来管理Action,包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action。在实际应用中,我们应该把一组业务功能相关的Action放在同一个包下;

 

 2、配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,他不对应java的类包,如果其他包要继承该包,必须通过该属性进行引用。包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下Action的路径的一部分,如访问上面例子的Action,访问路径为:/test/helloworld.action namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串);

 

3、通常每个包都应该继承struts-default包,因为Struts2很多核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件;

 

4、包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action

 

配置文件中的HelloWorldAction如下:

package cn.struts.demo;

public class HelloWorldAction {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String execute() {
		this.message = "我的第一个struts2应用";
		return "success";  // 对应struts配置文件中result视图
	}

}

 

 

配置文件中的hello.jsp文件如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我的第一个struts2应用</title>
</head>
<body>
	<!-- 用el表达式可以获取到action里面的值,此处的message说明action中有一个setMessage()的方法  -->
	${message } 
</body>
</html>

 

至此,我们的应用已开发完成,现在需要访问我们的HelloWorld应用,访问struts2actionURL路径由两部份组成:包的命名空间+action的名称,例如访问本例子HelloWorldActionURL路径为:/test/helloworld (注意:完整路径为:http://localhost:端口/内容路径/test/helloworld)。另外我们也可以加上.action后缀访问此Action。把项目(见附件)部署到tomcat下面,在地址栏中输入http://localhost:8080/strutsdemo/test/helloworld,页面显示       我的第一个struts2应用

 

流程:

1、首先客户端向服务端发出一个请求

2、请求经过StrutsPrepareAndExecuteFilter过滤器,过滤器init()方法会读取类路径下默认的配置文件struts.xml

3、在struts.xml中寻找对应的action,根据访问的路径,此处是name为helloworld的action

4、执行action中的execute(因为action中配的是method="execute")

5、执行execute方法,返回result name="success"的视图,即hello.jsp

6、页面显示    我的第一个struts2应用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值