Directory hierarchy
App Controller
AppController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.studyeasy.spring; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller("appController") public class AppController { private static final Logger logger = Logger.getLogger(AppController.class) ; @RequestMapping("/") public ModelAndView homepage() { ModelAndView model = new ModelAndView("index"); return model; } @RequestMapping("/admin") public ModelAndView admin() { logger.warn("User xyz accessed /admin URL"); ModelAndView model = new ModelAndView("admin"); return model; } @RequestMapping("/user") public ModelAndView user() { logger.info("User xyz accessed /user URL"); ModelAndView model = new ModelAndView("user"); return model; } } |
Spring Configuration File
SpringMVCConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<beans xmlns="http://www.springframework.org/schema/beans (http://www.spri ngframework.org/schema/beans)" xmlns:context="http://www.springframework.org/schema/context (http://www.s pringframework.org/schema/context)" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance (http://www.w3.org/20 01/XMLSchema-instance)" xmlns:mvc="http://www.springframework.org/schema/mvc (http://www.springfra mework.org/schema/mvc)" xsi:schemaLocation="http://www.springframework.org/schema/mvc (http://www. springframework.org/schema/mvc) http://www.springframework.org/schema/mvc/ spring-mvc-4.3.xsd (http://www.springframework.org/schema/mvc/spring-mvc4.3.xsd) http://www.springframework.org/schema/beans (http://www.springframework.or g/schema/beans) http://www.springframework.org/schema/beans/spring-beans-3 .0.xsd (http://www.springframework.org/schema/beans/spring-beans-3.0.xsd) http://www.springframework.org/schema/context (http://www.springframework. org/schema/context) http://www.springframework.org/schema/context/spring-c ontext-3.0.xsd (http://www.springframework.org/schema/context/spring-conte xt-3.0.xsd)"> <!-- Adding Support for Component Scan --> <context:component-scan base-package="org.studyeasy.spring" /> <!-- Configure View Resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewReso lver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:annotation-driven></mvc:annotation-driven> </beans> |
log4j.properties
log4j.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# logger enabled options log4j.rootLogger=INFO, stdout, file #Â Â Configuration for console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} | % p | Message: %m %n %n # Configuration for log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=${catalina.home}/logs/AppLogs.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} | %p | Message: %m %n %n |
Standard log levels
Standard Level | intLevel |
---|---|
OFF | 0 |
FATAL | 100 |
ERROR | 200 |
WARN | 300 |
INFO | 400 |
DEBUG | 500 |
TRACE | 600 |
ALL | Integer.MAX_VALUE |
Admin.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www .w3.org/TR/html4/loose.dtd (http://www.w3.org/TR/html4/loose.dtd)"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello Admin</title> </head> <body> Hello Admin! </body> </html> |
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www .w3.org/TR/html4/loose.dtd (http://www.w3.org/TR/html4/loose.dtd)"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>HomePage</title> </head> <body> Hello World! </body> </html> |
user.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core (http://java. sun.com/jsp/jstl/core)" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www .w3.org/TR/html4/loose.dtd (http://www.w3.org/TR/html4/loose.dtd)"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello User</title> </head> <body> Hello User! </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance (http://www. w3.org/2001/XMLSchema-instance)" xmlns="http://java.sun.com/xml/ns/j2ee (h ttp://java.sun.com/xml/ns/j2ee)" xmlns:web="http://xmlns.jcp.org/xml/ns/ja vaee (http://xmlns.jcp.org/xml/ns/javaee)" xsi:schemaLocation="http://xmln s.jcp.org/xml/ns/javaee (http://xmlns.jcp.org/xml/ns/javaee) http://java.s un.com/xml/ns/javaee/web-app_2_5.xsd (http://java.sun.com/xml/ns/javaee/we b-app_2_5.xsd) http://java.sun.com/xml/ns/j2ee (http://java.sun.com/xml/n s/j2ee) http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd (http://java.sun.c om/xml/ns/j2ee/web-app_2_4.xsd)" id="WebApp_ID" version="2.4"> <servlet> <servlet-name>SpringSample</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:org/studyeasy/spring/config/SpringMVCConfig.xml</pa ram-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringSample</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app> |