Struts 2中的注释支持

时间:2009-04-24 17:28:00

标签: java struts2 web-frameworks

我目前正在评估Struts 2.官方文档包含一个带有以下Java和JSP代码的HelloWorld示例:

爪哇

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

    public static final String MESSAGE = "Struts is up and running ...";

    public String execute() throws Exception {
        setMessage(MESSAGE);
        return SUCCESS;
    }

    private String message;

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

    public String getMessage() {
        return message;
    }
}

JSP

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h2><s:property value="message" /></h2>
    </body>
</html>

该示例使用以下XML文件为此操作指定URL,并将操作和视图绑定在一起

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="tutorial" extends="struts-default">
        <action name="HelloWorld" class="tutorial.HelloWorld">
            <result>/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

我的问题(最后)是:这真的是Struts2的最新技术吗?我一直认为,不推荐使用XML配置和子类化特定基类(ActionSupport)的必要性。我认为注释可以用于配置。

一些更具体的问题:

  • 是否需要为每个请求类型设置一个Action类,或者是否可以在eaction中定义多个方法,每个方法都可以处理不同的请求?
  • 是否有必要为要提供给视图的数据成员定义公共getter和setter(上例中的“message”)?

如果这是Struts2中最先进的技术,那么它对Struts1似乎没什么改进,但我怀疑这只是一个不好的例子。

谢谢, 唐

3 个答案:

答案 0 :(得分:1)

一些答案​​(不是全部):

  • 您可以向同一个Action类添加多个方法。每个都需要返回一个String。
  • 我认为需要公众的吸气剂和制定者。
  • 提供的基类仅定义某些有用的值,例如常量SUCCESS。

您可以通过“零配置”功能(zero conf doc

使用注释

答案 1 :(得分:1)

示例是旧式struts2。你可以使用几乎所有内容的注释,如@pgb所说。

还有2种不同的配置风格;一个叫做“约定”,另一个叫“注释”。约定受rails影响,并通过路径/命名在操作和结果之间创建默认绑定。使用注释可以声明所有内容,就像使用xml一样,但只能在控制器类中声明。我发现两者都是很好的简化,目前我只使用一个小型xml文件来运行大型网站。

答案 2 :(得分:1)

很抱歉找不到资源,但我无法复制并粘贴这些资源。

但这应该可以让您了解2.1.x的功能。

struts.xml中:

<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- Include webwork default (from the Struts JAR). -->
    <include file="struts-default.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="appLogin" class="us.pdinc.client.com.usgoldnetwork.support.struts.LoginInterceptor" />   
            <interceptor-stack name="loginStack">
                <interceptor-ref name="appLogin"/>
                <interceptor-ref name="paramsPrepareParamsStack"/>
            </interceptor-stack>
        </interceptors>     
        <!-- Default interceptor stack. -->
        <default-interceptor-ref name="loginStack"/>        
        <global-results>
            <result name="login">/WEB-INF/jsp/login-input.jsp</result>
        </global-results>
    </package>
</struts>
<!--  http://struts.apache.org/2.1.6/docs/convention-plugin.html  -->

struts.properties:

struts.custom.i18n.resources=guest
struts.convention.result.path=/WEB-INF/jsp
struts.ognl.allowStaticMethodAccess=true

AddrAction.java

    package us.pdinc.client.com.usgoldnetwork.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Session;
import org.hibernate.Transaction;

import us.pdinc.client.com.usgoldnetwork.beans.model.hibernate.Addr;
import us.pdinc.client.com.usgoldnetwork.beans.model.hibernate.AddrType;
import us.pdinc.client.com.usgoldnetwork.beans.model.hibernate.Tx;
import us.pdinc.client.com.usgoldnetwork.servlet.support.RequestScopeHibernateSession;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Results({
      @Result(name="list", type="redirect", location="/addr", params={"transaction.id","${transaction.id}"})
    })
public class AddrAction extends ActionSupport implements Preparable , ServletRequestAware 
{
    private static final long serialVersionUID = 1L;

    private List<Addr> addrs;
    private List<AddrType> addrTypes;
    private List<Tx> txs;
    private Addr addr;
    private AddrType addrtype;
    private HttpServletRequest httpServletRequest;
    private Tx transaction;
    long txId = 0;

    @Override
    public void prepare() throws Exception
    {
        if (addr!=null && addr.getId()!=null) 
            {
                Session session = RequestScopeHibernateSession.getSession(httpServletRequest);
                addr=(Addr) session.get(Addr.class, addr.getId());
            }
    }

    public String index()
    {
        return list();
    }

    public String list()
    {
        System.out.println("LIST");
        if (transaction==null)throw new RuntimeException();
        if (transaction.getId()==null) throw new RuntimeException();
        if (transaction.getId()<=0) throw new RuntimeException();
        Session session = RequestScopeHibernateSession.getSession(httpServletRequest);
        transaction=(Tx)session.get(Tx.class, transaction.getId());
        addrs=transaction.getAddrs();

        return SUCCESS;
    }

    public String input() 
    {
        Session session = RequestScopeHibernateSession.getSession(httpServletRequest);
        addrTypes=session.createCriteria(AddrType.class).list();
        //txs = session.createCriteria(Tx.class).list();
        return INPUT;
    }

    public String save() 
    {
        System.out.println("SAVE");

        if(transaction != null && transaction.getId() != null){
            txId = transaction.getId();
        }
        if(addr != null && addr.getTx() != null && addr.getTx().getId() != null){
            txId = addr.getTx().getId();
        }
        /*if (transaction==null || addr == null)throw new RuntimeException();
        System.out.println("******************************************************************");
        System.out.println("Transaction value ==" + transaction);
        System.out.println("Addr value ==" + addr);
        if (addr.getTx() == null) throw new RuntimeException();
        System.out.println("Transaction ID value ==" + transaction.getId());
        System.out.println("Addr Transaction value ==" + addr.getTx());
        if ( addr.getTx().getId() == null ) throw new RuntimeException();
        System.out.println("********************************************************************");
        System.out.println("Addr Transaction ID value ==" + addr.getTx().getId());*/

        Session session = RequestScopeHibernateSession.getSession(httpServletRequest);
        transaction=(Tx)session.get(Tx.class, txId);
        //String addrTypeId = httpServletRequest.getParameter("addr.addrType.id");
        //Long Id = Long.valueOf(addrTypeId);
        addrtype=(AddrType) session.get(AddrType.class, addr.getAddrType().getId());
        if (addr.getId()==null)
        {   
            System.out.println("Addr is null");
            addr.setTx(transaction);
            addr.setAddrType(addrtype);
            Transaction tx = session.getTransaction();
            tx.begin();
            session.save(transaction);
            session.persist(addr);
            tx.commit();
        }
        else
        {
            System.out.println("Addr is not null");
            Transaction tx = session.getTransaction();
            tx.begin();
            session.update(addr);
            tx.commit();        
        }
        return "list";
    }



    public String delete() 
    {
        Session session = RequestScopeHibernateSession.getSession(httpServletRequest);
        Transaction tx = session.getTransaction();
        tx.begin();
        session.delete(session.get(Addr.class, addr.getId()));
        tx.commit();        
        return "list";
    }


    @Override
    public void setServletRequest(HttpServletRequest arg0)
    {
        httpServletRequest=arg0; 
    }

    public Addr getAddr()
    {
        return addr;
    }

    public void setAddr(Addr addr)
    {
        this.addr = addr;
    }

    public List<Addr> getAddrs()
    {
        return addrs;
    }

    public List<AddrType> getAddrTypes()
    {
        return addrTypes;
    }

    public List<Tx> getTxs()
    {
        return txs;
    }

    public Tx getTransaction() {
        return transaction;
    }

    public void setTransaction(Tx transaction) {
        this.transaction = transaction;
    }

}

addr.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link href="<s:url value='/css/main.css'/>" rel="stylesheet" type="text/css"/>
    <title>Addresses Screen</title>
       <jsp:include page = "barcodeextractor.jsp" />
</head>
<body>
<div class="titleDiv">USGN Core Process</div>
<h1>Addresses</h1>
<s:url id="url" action="addr!input" >
<s:param name="transaction.id" value="transaction.id" />
</s:url>
<a href="<s:property value="#url"/>">Add New Address</a>
<br/><br/>
<table class="borderAll">
    <tr>
        <th>Customer</th>
        <th>Name</th>
        <th>Type</th>
        <th>Street 1</th>
        <th>Street 2</th>
        <th>Street 3</th>
        <th>Street 4</th>
        <th>Street 5</th>
        <th>Street 6</th>
        <th>City</th>
        <th>State</th>
        <th>Postal</th>
        <th>Country</th>
        <th>&nbsp;</th>
    </tr>
    <s:iterator value="addrs" status="status">
        <tr class="<s:if test="#status.even">even</s:if><s:else>odd</s:else>">
            <td class="nowrap"><s:property value="tx.id"/></td>
            <td class="nowrap"><s:property value="name"/></td>
            <td class="nowrap"><s:property value="addrType.addrType"/></td>
            <td class="nowrap"><s:property value="street1"/></td>
            <td class="nowrap"><s:property value="street2"/></td>
            <td class="nowrap"><s:property value="street3"/></td>
            <td class="nowrap"><s:property value="street4"/></td>
            <td class="nowrap"><s:property value="street5"/></td>
            <td class="nowrap"><s:property value="street6"/></td>
            <td class="nowrap"><s:property value="city"/></td>
            <td class="nowrap"><s:property value="state"/></td>
            <td class="nowrap"><s:property value="postal"/></td>
            <td class="nowrap"><s:property value="country"/></td>
            <td class="nowrap">
                <s:url action="addr!input" id="url">
                    <s:param name="addr.id" value="id"/>
                </s:url>
                <a href="<s:property value="#url"/>">Edit</a>
                &nbsp;&nbsp;&nbsp;
                <s:url action="addr!delete" id="url" >
                    <s:param name="addr.id" value="id"/>
                </s:url>
                <a href="<s:property value="#url"/>">Delete</a>
            </td>
        </tr>
    </s:iterator>
    </table>
</body>
</html>

地址-的Input.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:if test="addr==null || addr.id == null">
    <s:set name="title" value="%{'Add new contact type'}"/>
</s:if>
<s:else>
    <s:set name="title" value="%{'Update contact type'}"/>
</s:else>

<html>
<head>
    <link href="<s:url value='/css/main.css'/>" rel="stylesheet" type="text/css"/>
    <style>td { white-space:nowrap; }</style>
    <title><s:property value="#title"/></title>
     <jsp:include page = "barcodeextractor.jsp" />
</head>
<body>
<div class="titleDiv">USGN Core Process</div>
<h1><s:property value="#title"/></h1>
<s:actionerror />
<s:actionmessage />
<s:form action="addr!save" method="post">
    <s:select name="addr.addrType.id" label="Address Type" list="addrTypes" listKey="id" listValue="addrType" value="%{addr.addrType.id}"/>
    <s:textfield name="addr.name" value="%{addr.name}" label="Name" size="40"/>
    <s:textfield name="addr.street1" value="%{addr.street1}" label="Street 1" size="40"/>
    <s:textfield name="addr.street2" value="%{addr.street2}" label="Street 2" size="40"/>
    <s:textfield name="addr.street3" value="%{addr.street3}" label="Street 3" size="40"/>
    <s:textfield name="addr.street4" value="%{addr.street4}" label="Street 4" size="40"/>
    <s:textfield name="addr.street5" value="%{addr.street5}" label="Street 5" size="40"/>
    <s:textfield name="addr.street6" value="%{addr.street6}" label="Street 6" size="40"/>
    <s:textfield name="addr.city" value="%{addr.city}" label="City" size="40"/>
    <s:textfield name="addr.state" value="%{addr.state}" label="State" size="40"/>
    <s:textfield name="addr.postal" value="%{addr.postal}" label="Postal" size="40"/>
    <s:textfield name="addr.country" value="%{addr.country}" label="Country" size="40"/>
    <s:hidden name="addr.id" value="%{addr.id}"/>
    <s:hidden name="transaction.id" value="%{transaction.id}"/>
    <s:submit value="Submit"/>
    <s:submit value="Cancel" name="redirectAction:addr!list"/>
</s:form>
</body>
</html>