siye@r480:~/svlution/workspace/springmvc4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springmvc
│ │ ├── controller
│ │ │ └── Manicontroller.java
│ │ ├── setup
│ │ │ ├── Appconfig.java
│ │ │ └── Webxmlconfig.java
│ │ └── utils
│ │ └── ConstraintsUtils.java
│ ├── resources
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ ├── error.jsp
│ └── web.xml
└── test
├── java
└── resources
14 directories, 8 files
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
package ocn.site.springmvc.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;
@Controller
public class Manicontroller {
private final Logger logger = Logger.getLogger(this.getClass());
@GetMapping("/test")
public void handler() throws ServletException {
logger.info("running");
throw new ServletException("test tag of error page");
}
@GetMapping("/error")
public ModelAndView handlerException(HttpServletRequest request) {
ModelAndView mav = new ModelAndView("error");
mav.addObject("status", request.getAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE));
mav.addObject("message", request.getAttribute(WebUtils.ERROR_MESSAGE_ATTRIBUTE));
mav.addObject("exception", request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE));
return mav;
}
}
package ocn.site.springmvc.setup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import ocn.site.springmvc.utils.ConstraintsUtils;
@Configuration
@EnableWebMvc
@ComponentScan({ "ocn.site.springmvc.controller", "ocn.site.springmvc.domain" })
public class Appconfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public ViewResolver getViewResolver() {
return new InternalResourceViewResolver(ConstraintsUtils.PREFIX, ConstraintsUtils.SUFFIX);
}
}
package ocn.site.springmvc.setup;
import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import ocn.site.springmvc.utils.ConstraintsUtils;
public class Webxmlconfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { Appconfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new CharacterEncodingFilter(ConstraintsUtils.ENCODING, true),
new HiddenHttpMethodFilter() };
}
}
package ocn.site.springmvc.utils;
public interface ConstraintsUtils {
String ENCODING = "UTF-8";
String PREFIX = "/WEB-INF/";
String SUFFIX = ".jsp";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<base href="${pageContext.request.contextPath }/">
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<script src="https://libs.cdnjs.net/jquery/3.4.1/jquery.min.js"></script>
<script src="https://libs.cdnjs.net/json2/20160511/json2.min.js"></script>
<title>Insert title here</title>
</head>
<body>
<p>status:${status }</p>
<p>message:${message }</p>
<p>exception:${exception }</p>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>springmvc4322</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<location>/error</location>
</error-page>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<base href="${pageContext.request.contextPath }/">
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<script src="https://libs.cdnjs.net/jquery/3.4.1/jquery.min.js"></script>
<script src="https://libs.cdnjs.net/json2/20160511/json2.min.js"></script>
<title>Insert title here</title>
</head>
<body>
<a href="test">test</a>
</body>
</html>