Analysis of the Spring MVC Principle

Introduction  

Spring MVC framework is one of the most famous and popular frameworks in Java programming. As a professional programmer, it is crucial to understand the principle behind it. This tutorial will briefly introduce Spring MVC and try to replicate the whole framework with POJO Java code. Let's do it!

Components of Spring MVC

Before diving into the concept of MVC, we need first to understand what is MVC:

M(Model): model contains the data of an application that can be transferred within different parts of the MVC system.

C(Controller): the controller is the logic or "brain" of the whole system. We will use some simple servlets to replicate the function of the Spring controller.

V(view): the view represents certain formats that will appear in clients' browsers. Current frequently used view formats are the Jsp and Thymeleaf etc.

Here below is a picture describing what Spring MVC framework looks like;

                               picture source: https://www.javatpoint.com/spring-mvc-tutorial

Configuration using pure Java

The first step is to set up proper Maven dependencies. Basically, what we need is Servlet, Tomcat and primary Spring context.

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
</dependency>
 <dependency>
            <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.36</version>
        </dependency>

After adequately inserting our dependencies, we will handle crucial components in this project. Here, we first need to establish our tomcat setting:

public class MainServer {

    private ApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(ApplicationConfig.class);

    public MainServer(){
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector();

        Context context = tomcat.addContext("",null);

        try {
            List<Class> list = ServletUtil.getClasses("com.project.servlet");
            for (Class  c : list){
                if(c.isAnnotationPresent(ServletObj.class)){
                    // To obtain objects that are annotated with our own @ServletObj
                    ServletObj servletObj = (ServletObj)c.getAnnotation(ServletObj.class);
                    this.registerServlet(context,c,servletObj.url());
                }
            }
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // define a method to register servlet 
    private void registerServlet(Context context,Class servletClass,String urlPath) throws Exception {
        Wrapper servlet = Tomcat.addServlet(
                context,
                servletClass.getSimpleName(),
                (Servlet) applicationContext.getBean(servletClass));
        servlet.setLoadOnStartup(1);
        servlet.addMapping(urlPath);
    }

    public static void main(String[] args) {
        new MainServer();
    }
}

First of all, the tomcat port is necessary. Then a context is added for this tomcat. To make things easier, we used a custom annotation called "@ServletObj" to identify which class is the servlet we want to add to our dispatcher. Here is the code of our ServletObj annotation using reflection.

public class ServletUtil {

    // scan the name of packages within typed packageName 
    public static List<Class> getClasses(String packageName){
        List<Class> list = new ArrayList<>();
        // obtain packages' name and replace with the name we want
        String packageDirName = packageName.replace(".","/");
        // obtain full URL
        try {
            URL url = Thread.currentThread().getContextClassLoader()
                    .getResources(packageDirName).nextElement();
            String filePath = URLDecoder.decode(url.getFile(),"utf-8");
            // obtain new object through filepath
            File dirFile = new File(filePath);
            String[] classFiles = dirFile.list();
            for(String f : classFiles) {
                if(f.endsWith(".class")){
                    String fileName = f.substring(0,f.lastIndexOf(".class"));
                    Class c = Class.forName(packageName + "." + fileName);
                    list.add(c);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}

The ServletUtil class displays how we can scan through the name of our package and obtain all classes underneath it. In this tutorial, we added servlets to our tomcat context and accessed static resources. For our Spring dispatch configuration, we will leave it to the following tutorial. I can't wait to see you next time!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值