Struts2-第一篇-HelloWorld

来源:互联网 发布:java计算器界面代码 编辑:程序博客网 时间:2024/05/17 00:19

当你点击一个超级链接或者在一个Html中点击一个"submit"在一个Struts2的web应用,不是转向另外一个页面,而是转向你提供的Action类。Action被调用后,Result会选择一个资源去响应。这些资源一般是一个page,当然也可以是PDF,excel或者一个JavaAppletwindow。

下面通过创建一个HelloWorld的例子显示一个欢迎信息,首先创建一个Struts2的web应用。创建一个HelloWorld例子,需要四件事情:

1、创建一个类存储欢迎信息(the mode)

2、创建一个页面显示信息(the view)

3、创建一个Action控制user,mode,view之间的关系

4、创建一个mapping(struts.xml)映射Action和view

step 1 - 创建Modeclass MessageStore.java

在src下创建一个类MessageStore,在包org.apache.struts.helloworld.mode

package org.apache.struts.helloworld.model;/* * the mode */public class MessageStore {private String message;public MessageStore(){setMessage("Hello Struts2 User");}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}

step 2 -创建一个Action类HelloWorldAction.java

我们需要一个Action类作为控制器。Action类相应一个用户action。一个或者更多的Action方法被执行并且一个string result被返回。基于result值,一个view page被显示。

package org.apache.struts.helloworld.action;import org.apache.struts.helloworld.model.MessageStore;import com.opensymphony.xwork2.ActionSupport;/* * the control */public class HelloWorldAction extends ActionSupport {private static final long serialVersionUID = 1L;private MessageStore messageStore;@Overridepublic String execute() throws Exception {messageStore = new MessageStore();return SUCCESS;}public MessageStore getMessageStore() {return messageStore;}public void setMessageStore(MessageStore messageStore) {this.messageStore = messageStore;}}

Struts2框架将创建一个HelloWorldAction类对象并且调用execute方法响应给用户action,例如:点击一个url链接,在这个action中,执行execute方法,创建MessageStore对象并返回字符串常量。

step 3-创建一个jsp页面

我们需要一个服务器页面来显示被存储在mode类MessageStore中的信息。创建一个jsp页面。

Struts2 taglib标签命令告知Servlet容器,这个页面将用到Struts2标签,并且这些标签以s引导。

s:property标签显示返回的值,该值是通过调用HelloWorldAction控制类的getMessageStore方法。这个方法返回一个MessageStore对象,通过添加message到MessageStore值属性。MessageStore的getMessage方法返回一个字符串。字符串将通过s:property标签显示。

step 4 -添加一个Struts配置在struts.xml

我们需要一个mapping把URL,HelloWorldAction,HelloWorld.jsp联系到一起。Mapping负责告诉框架哪个类将响应用户的action,哪个方法将被执行,并且view负责表现Result和method的返回。

编辑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>   <constant name="struts.devMode" value="true" />   <package name="basicstruts2" extends="struts-default">   <action name="index">    <result>/index.jsp</result>  </action>           <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">    <result name="success">/HelloWorld.jsp</result>  </action> </package> </struts>

step 5 - 创建URL action

在index.jsp中,让我们添加一个action url,用户点击告诉框架去运行HelloWorldAction类的execute方法,并且表现在helloworld.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags" %><!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=ISO-8859-1"><title>Basic Struts 2 Application - Welcome</title></head><body><h1>Welcome To Struts 2!</h1><p><a href="<s:url action='hello'/>">Hello World</a></p></body></html>

struts url tag创建URL,hello action事件。hello action被mapped到HelloWorldAction类并且执行它的方法。当用户点击url会引起框架运行execute方法。返回success字符串,HelloWorld.jsp页面显示。

Step 6 部署项目并运行程序打开浏览器,输入:

http://localhost:8080/helloworld/index.action


点击 HelloWorld,进入helloworld.jsp页面


0 0