Archives: Struts

Java Struts – Logging with Log4j Example

Download Log4j: http://logging.apache.org/log4j/index.html

Log4j Appender:
Create a Log4j properties file (log4j.properties) and define the appender details (where to log and log file details). And put the log4j.properties into your project classpath.

log4j.properties

Struts Action Class:

Struts Custom Exception Handler:

Log4j Output File Sample- c:\\logging.log


Java Struts – Handle HTTP 404 Error

The “HTTP Status 404” error code shows that the system can not find the page or resource you requested. In practice, you should display your custom 404 error page. However, it is handle in web.xml , not in Struts framework. For example,
[code lang=”xml”]<web-app>

<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>[/code]

When system hit the 404 error, it will forward to your custom 404 error page “/pages/error404.jsp“.

struts-config.xml
[code lang=”xml”]<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Maven Struts Examples</display-name>

<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>

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

<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>[/code]