⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newsaction.java

📁 为Web服务构建Struts应用程序例程源码,好东西
💻 JAVA
字号:
/**
 * An Action class is an adapter between the contents of an incoming HTTP request
 * and the corresponding business logic that should be executed to process
 * this request. The controller (ActionServlet) will select an appropriate
 * Action for each request, create an instance (if necessary), and call the
 * perform method.
 *
 * Actions must be programmed in a thread-safe manner, because the controller
 * will share the same instance for multiple simultaneous requests.
 *
 * The News Action gets all the required field based on which event is fires
 * from the request. It gets the required data by calling the relevent
 * Business Services. The data is again populated back in html by using the
 * setters in the action form.
 *
 *
 */

/**
 * Change History:
 * Author               Date            Version   Details
 * Jerome Josephraj  28 October 2002    1.00.01   Created
 */

package com.ddj.wsstruts.action;

//Java imports
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Vector;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.sql.Connection;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;

//Struts imports
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;

//Project imports
import com.ddj.wsstruts.ms.NewsMs;
import com.ddj.wsstruts.valueobject.NewsSearchResult;
import com.ddj.wsstruts.actionform.NewsForm;
import com.ddj.wsstruts.constant.SystemConstants;
import com.ddj.wsstruts.wsmanager.subscriber.StocksWSManager;

//Log4J statements
import org.apache.log4j.Category;
import org.apache.log4j.xml.DOMConfigurator;

public final class NewsAction extends Action {
    
      
    //Initialise Log4J DOMConfigurator
    static
    {
        DOMConfigurator.resetConfiguration();
        DOMConfigurator.configure(SystemConstants.DOM_CONFIGURATOR_FILE);
    }	
    //Log4J
    // define a static category variable so that it references the
    // category instance of the same name as this class.
    static final Category category = Category.getInstance(NewsAction.class.getName());
    NewsMs newsMs = null;
    HttpSession session = null;
    
    
    // --- Public Methods ---
    
    /*
     * Process the specified HTTP request, along with the ActionForm.
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     * Any exceptions are handled in StrutsActionBase class. If exceptions have 
     * to be made process specific, catch the exception here and do the
     * relevant process.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param actionForm The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs testing
     */
    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException,ServletException {
        Vector stockValues = null;
        NewsSearchResult newsSearchResultVOB = null;
        Vector newsList             = null;
        NewsForm newsFrm            = null;
        ActionErrors errors         = null;
        ActionForward actionForward = null;
        String action               = null;
        StocksWSManager stockWSManager = null;

        try
        {
            if(category.isDebugEnabled()) {
                category.info(SystemConstants.METHOD_START);
            }

            newsMs = new NewsMs();

            //Get session from request
            session = request.getSession(false);

            //Cast the form to NewsForm object
            newsFrm = (NewsForm)form;

            // Retrieve values from request
            action = newsFrm.getHdnProcessAction();

            /*
             * log4j Debugging method, which prints onto the console
             */
            if(category.isDebugEnabled()) {
                category.info("inside action");
            }

            //get newscontent from the database
            actionForward= getNews(mapping,newsFrm,request,response);

            if(category.isDebugEnabled()) {
                category.info("action formward is : " + actionForward);
            }


            //Get the stock values by calling the webservice manager
            if(category.isDebugEnabled()) {
                category.info("Before calling webservice manager");
            }

            stockWSManager = new StocksWSManager();

            //Call the subscriber webservice manager, to get stock values
            //from a webservice
            //If an error occurs in web service manager, it throws an exception
            stockValues = stockWSManager.getStocks();

            if(category.isDebugEnabled()) {
                category.info("After calling webservice manager");
            }

            //Set the stockValues in the form
            newsFrm.setStockValuesList( (Vector) stockValues);

            //Set the form in session
            session.setAttribute("newsForm",newsFrm);
        }
        catch(Exception e) {
            
            if(category.isDebugEnabled()) {
                category.error(e.getMessage());
            }
            
            //if the exception is from webservice, set the webserviceerror in the request
            //and forward it to the NewsContent jsp page. This would allow the jsp page to 
            //display an appropriate error message (service not available)
            if (e.getMessage().equals(SystemConstants.WEBSERVICE_EXCEPTION)) {
                //Set a value in an attribute in request. This will be checked
                //in the jsp page to display the appropriate messages
                request.setAttribute("webserviceerror",SystemConstants.WEBSERVICE_EXCEPTION);
                
                //Return to the NewsDetails page
                return mapping.findForward("success");
                
            }
            else {
                //Forward it to an error page
                return mapping.findForward("failure");
            }
        }

        // Forward control to the specified success URI
        return actionForward;
    }
    
    /*
     * Process the specified HTTP request, along with the ActionForm.
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     * This method is used to get newscontent from the database. To get
     * NewsContent from the database it calls the Model service, and the model 
     * service gets the result from the database and sends a populated 
     * pre-defined data access object.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param actionForm The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception ValidationException if any mandatory data is missing
     * @exception ApplicationException if any particular exceptions caused by null objects.
     * @exception Exception if any exception other than the above
     */
    private ActionForward getNews(ActionMapping mapping,NewsForm newsFrm,HttpServletRequest request,HttpServletResponse response)
            throws Exception {
        Vector newsList = null;
        newsMs = new NewsMs();
        NewsSearchResult newsSearchResultVOB = null;
        
        if(category.isDebugEnabled()) {
            category.debug("Inside SEARCHACTION "+mapping.getInput());
        }
        
        newsSearchResultVOB = new NewsSearchResult();
        
        /*
         * Call Business layer NewsMs's searchNews method passing the NewsCOD
         * and get back a Collection of News
         */
        newsList = (Vector) newsMs.getNews(newsSearchResultVOB);
        
        // tag to determine whether to display the Collection of News or not
        //newsFrm.setNewsList(newsList);
        
        /*
         * Put the NewsList in the request.
         *
         */
        request.setAttribute(SystemConstants.NEWS_LIST, newsList);
        
        //Set the newslist in the form
        newsFrm.setNewsDetails(newsList);
        
        if(category.isDebugEnabled()) {
            category.debug("mapping.getAttribute() "+mapping.getAttribute()+" "+mapping.getInput()+" "+newsList.size());
        }
        
        // Set the NewsForm in the request
        request.setAttribute(mapping.getAttribute(), newsFrm);
        
        //return actionForward;
        return mapping.findForward("success");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -