Configuring log4j logs in the SSH framework and STRUTS2 configuration exception handling methods

Source: Internet
Author: User
Tags exception handling log4j
1. File Configuration Example
# Default properties to initialise log4j

log4j.appender.a1= Org.apache.log4j.ConsoleAppender
Log4j.appender.a1.layout.conversionpattern=[%d{mm-dd HH:mm:ss}]%-5p%c{1} [%t]:%m%n

# file
log4j.appender.file.file=${webapp.root}/web-inf/ Log/sshtest.log

2. web.xml File Configuration Example
<web-app version= "3.0" xmlns= Http:// "xmlns:xsi=" Xmlschema-instance "xsi:schemalocation=" Http:// Web-app_3_0.xsd "> <display-name></display-name> <welcome-file-list> <welcome-file>inde x.jsp</welcome-file> </welcome-file-list> <filter> &LT;FILTER-NAME&GT;STRUTS2&LT;/FILTER-NAME&G
  	T <filter-class> Org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </ Filter-mapping> <!--pay attention to the order of each listener, otherwise there may be the above ${webapp.root}/web-inf/log/sshtest.log anomaly--> <!--log4j con Fig path--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/ web-inf/classes/</param-value&Gt </context-param> <!--webapp Root path--> <context-param> <param-name>webapprootkey</p Aram-name> <param-value>webapp.root</param-value> </context-param> <!--spring Config path --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/web-i
  Nf/classes/applicationcontext.xml</param-value> </context-param> <!--log4j Config listener-->
  <listener> <listener-class> Org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!--spring Contextloader listener--> <listener> <listener-class> org. Springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>

3. struts2.xml File Configuration Example
The following configuration allows you to automatically go to the Exception display page when an unexpected exception is applied; Note the order of each node
<struts> <package name= "its" extends= "Struts-default" > <interceptors> <interceptor-stack name= "Globalstack" > <interceptor-ref name= "defaultstack" > <param name= "exception.logenabled" >true</p aram> <param name= "Exception.loglevel" >ERROR</param> </interceptor-ref> </interceptor- stack> </interceptors> <default-interceptor-ref name= "Globalstack" ></default-interceptor-ref > <global-results> <result name= "error" >/error.jsp</result> </global-results> <glob Al-exception-mappings> <!--Here you can set up multiple exception types--> <exception-mapping result= "error" exception= " Java.lang.Exception "></exception-mapping> </global-exception-mappings> <action name=" Login " class= "Com.inspur.its.action.LoginAction" > <result name= "Success" >/Result.jsp</result> </action    
 > </package> </struts>

4. error.jsp page sample (only show the body part, import the struts2 tablib)
	<div align= "CENTER" >
An example of a truncated diagram following an exception appears:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.