-->

NullPointer Exception in Struts application

2019-08-28 01:12发布

问题:

I want to create a struts app which displays index.jsp file which contains link to the search.jsp file.Directory structure of Struts 1.2 app is as below

Struts 1.2 Directory Listing

The content of web.xml is as below

      <display-name>MiniHR</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>  
  <jsp-config>
   <taglib>
    <taglib-uri>http://struts.apache.org/tags-html</taglib-uri>
    <taglib-location>/WEB-INF/lib/struts-html.tld</taglib-location>    
  </taglib>
   </jsp-config>
   <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.</url-pattern>
    </servlet-mapping>
   <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>
    <load-on-startup>1</load-on-startup>
</servlet>
</web-app>

The contents of struts-config.xml is as below

<global-forwards>
    <forward name="search" path="/search.jsp"></forward>
</global-forwards>

The content of index.jsp is as below

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<body>
    Add Employee<html:link forward="search">Search Employees</html:link>
</body>

Now when i run this app on eclipse its displaying HTTP Status 500 - as below

I tried few solutions but nothing seems to fix the problem.A Little Help would be appreciated.

I have added the war file in following URL

http://www.fileconvoy.com/dfl.php?id=g6daddfa41e8981249992832312c465146f9b6bc45

Thank you

回答1:

Struts assumes that all requests go through the Struts ActionServlet, which you aparently have not configured. Set up the Struts servlet with servlet-mapping *.do:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Then you need to define some action mappings in your struts.xml that redirects to your view. You can't access the JSPs directly if you're using struts tags in your JSP.

Edit

You have updated your question with new web.xml content, so the above is covered. However, you still can't access the JSPs directly. You need to add an action mapping that forwards to your view.



回答2:

You have html:link tag attribute forward wrong.

According to the Struts link tag specification:

forward - Use the value of this attribute as the name of a global ActionForward to be looked up, and use the module-relative or context-relative URI found there. If the forward is module-relative then it must point to an action and NOT to a page.

So, your global forward should point to a Struts Action and not a page.

I hope this resolves your problem.

See a related example here.