Spring MVC 框架能够分离模块,即模型、视图和控制器,并无缝处理应用程序集成。这使开发人员也可以使用普通的 java 类来创建复杂的应用程序。模型对象可以使用映射在视图和控制器之间传递。在本文中,我们将了解如何在 Eclipse IDE 中设置 Spring MVC 应用程序并了解如何制作应用程序。
Spring MVC 框架由以下组件组成:
要求 :
在 Eclipse IDE 中设置 Spring MVC 应用程序的步骤:
4.0.0 com.geeksforgeeks SpringMVCjava0.0.1-SNAPSHOT war SpringMVCjava Maven Webapp http://www.example.com UTF-8 1.7 1.7 junit junit4.11 test org.springframework spring-core5.2.9.RELEASE org.springframework spring-web5.2.9.RELEASE org.springframework spring-webmvc5.2.9.RELEASE SpringMVCjava maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
package com.geeksforgeeks.web;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { MVCconfig.class };
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] { "/" };
}
}
package com.geeksforgeeks.web;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@ComponentScan({ "com.geeksforgeeks.web" })
public class MVCconfig extends WebMvcConfigurerAdapter {
}
package com.geeksforgeeks.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class GreetController {
@RequestMapping("/greet")
public ModelAndView showview() {
ModelAndView mv = new ModelAndView();
mv.setViewName("result.jsp");
mv.addObject("result", "GeeksForGeeks Welcomes " + "you to Spring!");
return mv;
}
}
Hello World!
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%>Insert title here ${result}
当 index.jsp 打开时:
按下按钮时:



