Struts2学习笔记——Action

来源:互联网 发布:网络规划设计师 没人考 编辑:程序博客网 时间:2024/04/30 00:51
具体视图的返回可以由用户自己定义的Action来决定
具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容
具体Action的实现可以是一个普通的java类,里面有public String execute方法即可
或者实现Action接口

不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法

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>  <constant name="struts.devMode" value="true" />  <package name="front" namespace="/" extends="struts-default">       <!-- 配置默认的访问路径 -->        <action name="index" class="com.smile.struts2.front.action.IndexAction1">            <result> /ActionIntroduction.jsp  </result>        </action>    </package>        <package name="main" namespace="" extends="struts-default">       <!--  <default-action-ref name="index" /> -->       <!-- 配置默认的访问路径 -->        <action name="index">            <result>                /Namespace.jsp            </result>        </action>    </package>      <!-- Add packages here --></struts>
IndexAction的实现可以是一个简单的java类:

package com.smile.struts2.front.action;public class IndexAction1 {public String execute() throws Exception{return "success";}}
也可以是一个实现了Action接口的类:

package com.smile.struts2.front.action;import com.opensymphony.xwork2.Action;public class IndexAction2 implements Action {public String execute() throws Exception {return SUCCESS;}}
最常用的是继承自ActionSupport:

package com.smile.struts2.front.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction3 extends ActionSupport {public String execute(){return SUCCESS;}}
这几个执行效果都是一样的。

ActionIntroduction.jsp:

<?xml version="1.0" encoding="GB18030" ?><%@ page language="java" contentType="text/html; charset=GB18030"    pageEncoding="GB18030"%>    <%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030" /><title>Insert title here</title></head><body>具体视图的返回可以由用户自己定义的Action来决定<br />具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容<br />具体Action的实现可以是一个普通的java类,里面有public String execute方法即可<br />或者实现Action接口<br />不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法<br /></body></html>



0 0
原创粉丝点击