Spring Web MVC-01-quick start
2019年12月25日大约 3 分钟
Web MVC
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet
that dispatches requests to handlers,
with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files.
The default handler is based on the @Controller
and @RequestMapping
annotations, offering a wide range of flexible handling methods.

Hello World
The src files tree.
|____main
| |____java
| | |____com
| | | |____ryo
| | | | |____controller
| | | | | |____HelloController.java
| |____resources
| | |____app-mvc.xml
| | |____app.xml
| | |____log4j2.xml
| |____webapp
| | |____WEB-INF
| | | |____web.xml
- pom.xml
4.0.0
com.ryo
springmvc
1.0.0
war
UTF-8
2.2
2.18.1
3.3
4.2.6.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
4.2.6.RELEASE
org.springframework
spring-webmvc
4.2.6.RELEASE
org.apache.tomcat.maven
tomcat7-maven-plugin
${plugin.tomcat.version}
8080
/
${project.build.sourceEncoding}
org.apache.maven.plugins
maven-surefire-plugin
${maven-surefire-plugin.version}
true
true
org.apache.maven.plugins
maven-compiler-plugin
${maven-compiler-plugin.version}
1.8
1.8
- web.xml
contextConfigLocation
classpath:app.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:app-mvc.xml
1
springmvc
/*
- app.xml
- app-mvc.xml
- HelloController.java
package com.ryo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by houbinbin on 16/6/19.
*/
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String hello() {
return "hello world";
}
}
- visit
http://localhost:8080/hello
- result
hello world
Controller log
- pom.xml
org.apache.logging.log4j
log4j-api
${log4j.version}
org.apache.logging.log4j
log4j-core
${log4j.version}
org.apache.logging.log4j
log4j-web
${log4j.version}
- define controller call interceptor.
package com.ryo.interceptor;
import com.alibaba.fastjson.JSON;
import com.ryo.constants.SessionKeys;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by houbinbin on 16/6/30.
* - Controller 访问日志
*/
@Component
@Aspect
public class ControllerLogInterceptor {
static Logger logger = LogManager.getLogger();
private String requestPath = null; // 请求地址
private String userName = null; // 用户名
private Map inputParamMap = null; // 传入参数
private Map outputParamMap = null; // 存放输出结果
@Pointcut("execution(public * com.ryo.controller..*.*(..))")
public void myPointcut() {
}
@Before("myPointcut()")
public void start() {
}
@After("myPointcut()")
public void end() {
printLog();
}
@Around("myPointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
/**
* 1.获取request信息
* 2.根据request获取session
* 3.从session中取出登录用户信息
*/
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 从session中获取用户信息
userName = (String) request.getSession().getAttribute(SessionKeys.SESSION_USERNAME);
// 获取输入参数
inputParamMap = request.getParameterMap();
// 获取请求地址
requestPath = request.getRequestURI();
// 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
outputParamMap = new HashMap();
Object result = point.proceed(); // result的值就是被拦截方法的返回值
outputParamMap.put("result", result);
return result;
}
private void printLog() {
logger.info("USER:" + userName
+ " URL:" + requestPath + "\n"
+ "Param:" + JSON.toJSON(inputParamMap) + "\n" + "Result:" + JSON.toJSON(outputParamMap)+"\n");
}
}
贡献者
binbin.hou