?
Solved

Eclipse example with request data issues default output folder

Posted on 2012-08-15
6
Medium Priority
?
776 Views
Last Modified: 2012-08-19
Eclipse I am building one struts with request data example.

I went to file---dynamic web project---then gave name StrutsReqWithDataNoForm2--- source folders on build path src(Can I change this folder???)
---default outputlocation instead of 'build/classes' I gave as 'classes'(my intention is to see classes directory under WEB-INF/classes.

But after creating project under WEB-INF i do not see 'classes' folder(is it is invisible do i need make some changes to make it visible). I need to create 'actions' folder under 'classes' and eventually write a 'MaintenanceAction.java action class' there.
I wonder why I cannot see default output folder any where to do this. How can i see it and add following class to it to redirect to corresponding jsp based on the request parameter selection and action mapping.

/StrutsReqWithDataNoForm2/WEB-INF/classes/actions/MaintenanceAction.java
MaintenanceAction.java
package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MaintenanceAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String nextPage = "";
try {
// No From bean. So read the data from request.
String page = request.getParameter("page");
// If page is "sales", then it goes to the page whose forward is named
//with "sales"
nextPage = page;
} catch (Exception e) {
}
// Finish with
return mapping.findForward(nextPage);
}
}

I already created 2 jsps called Sales.jsp and Inventory.jsp






package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MaintenanceAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String nextPage = "";
try {
// No From bean. So read the data from request.
String page = request.getParameter("page");
// If page is "sales", then it goes to the page whose forward is named
//with "sales"
nextPage = page;
} catch (Exception e) {
}
// Finish with
return mapping.findForward(nextPage);
}
}



Sales.jsp
<HTML>

<h1>Welcome to Sales Page</h1>
</HTML>



Inventory.jsp
<HTML>
<h1>Welcome to Inventory Page</h1>
</HTML>

DefaultOutputFolder.docx
0
Comment
Question by:gudii9
  • 4
  • 2
6 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 38297336
0
 
LVL 7

Author Comment

by:gudii9
ID: 38297629
It worked fine as in the attachment. Following are the URLs I have used.

http://localhost:8080/StrutsReqWithDataNoForm2/maintenance.do?page=sales

http://localhost:8080/StrutsReqWithDataNoForm2/maintenance.do?page=inventory

http://localhost:8080/StrutsReqWithDataNoForm2/welcome.do

Struts-config file looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
      <!-- Data Sources -->
      <data-sources></data-sources>
      <!-- Form Beans -->
      <form-beans></form-beans>
      <!-- Global Exceptions -->
      <global-exceptions></global-exceptions>
      <!-- Global Forwards -->
      <global-forwards></global-forwards>
      <!-- Action Mappings -->
      <action-mappings>
            <action path="/welcome" forward="/jsps/struts/Welcome.jsp" />
<action path="/maintenance" type="actions.MaintenanceAction">
<forward name="sales" path="/jsps/struts/Sales.jsp" />
<forward name="inventory" path="/jsps/struts/Inventory.jsp" />
</action>
      </action-mappings>
      <!-- Message Resources -->
</struts-config>



web.xml looks like

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <display-name>Struts Test from coreservlets.com</display-name>
 
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


  <!-- The Usual Welcome File List -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

My question still remains why the build/classes folder is not visible. Even though I changed to classes then also it is invisible. Please advise
InventoryPAge.JPG
0
 
LVL 7

Author Comment

by:gudii9
ID: 38297671
I changed the Action class as below

instead of
//String page = request.getParameter("page");

I added below line
String page =      (String) request.getSession().getAttribute("page");      

It is not leading to corresponding Sales or Inventory page any more. It just dispays blank page. Please advise


package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MaintenanceAction extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
      throws Exception {
            String nextPage = "";
            try {
                  // No From bean. So read the data from request.
                  //String page = request.getParameter("page");
                  String page =      (String) request.getSession().getAttribute("page");
                  // If page is "sales", then it goes to the page whose forward is named
                  //with "sales"
                  nextPage = page;
            } catch (Exception e) {
            }
            // Finish with
            return mapping.findForward(nextPage);
      }
}

When I go to

http://localhost:8080/StrutsReqWithDataNoForm2/maintenance.do?page=inventory

Says cannot go to Null forward. Please advise
NullForward.JPG
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 38299205
Firstly, just a bit of house keeping; if you have a separate, non-related question you should open a new question, that way you will get more people looking at it. Just adding it as a comment to a different question will generally mean that nobody will see it.

As to the original question, the output directory is for compiled classes, you don't put you .java files there because there are only source files, and nothin will happen with them. You would put your MaintenanceAction.java in the src/actions folder. That way, Eclipse knows about it and will compile the class for you, which will be compiled into the output directory that you have specified. You don't even need to worry about where that is because Eclipse will make sure your classes are on the classpath for Tomcat when it starts up. That's the beauty about IDE's, they do that housekeeping stuff for you!

And as for the second question, it was working originally because you were retrieving the 'page' parameter from the request correctly. Then you changed to try and retrieve an attribute called 'page' from the session, and so it doesn't work because there is no 'page' attribute in the session (hence the error saying it can't find 'null'). Session attributes and request parameters are two totally different things. There won't ever be any 'page' attribute in the session until you put one there (in either the current request or a previous request).   Maybe you need to explain why you wanted to try and make that change in the first place? But what the code is doing seems absolutely correct and normal for the change that you made.
0
 
LVL 7

Author Comment

by:gudii9
ID: 38300703
>>>You don't even need to worry about where that is because Eclipse will make sure your classes are on the classpath for Tomcat when it starts up.


If I wish to change to default output directory do I have option?

Or I just have to accept default output directory as build/classes all the time when using eclipse. Please advise
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38309341
You can (and probably successfully did) change the output directory. It's just what you were trying to change it too, and the reasons that you were trying to change it were wrong!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question