首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Struts2-HelloWorld

2013-03-14 
Struts2--HelloWorld?xml version1.0 encodingUTF-8 ?!DOCTYPE struts PUBLIC-//Apache Softwar

Struts2--HelloWorld
<?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" name="code">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应用,访问struts2中action的URL路径由两部份组成:包的命名空间+action的名称,例如访问本例子HelloWorldAction的URL路径为:/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应用

?

热点排行