SpringMvc 的小dome

编译环境:win10 64位+jdk 11.0+eclipse+Tomcat 8.5

文件结构:

 

各文件源码

IndexController.java

 1 package controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 @Controller
11 public class IndexController {
12     @RequestMapping("/index")
13     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14         ModelAndView mav = new ModelAndView("index");
15         mav.addObject("message", "Hello Spring MVC");
16         return mav;
17     }
18 }

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>

<h1>${message}</h1>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6     <servlet>
 7         <servlet-name>springmvc</servlet-name>
 8         <servlet-class>
 9             org.springframework.web.servlet.DispatcherServlet
10         </servlet-class>
11         <load-on-startup>1</load-on-startup>
12     </servlet>
13     <servlet-mapping>
14         <servlet-name>springmvc</servlet-name>
15         <url-pattern>/</url-pattern>
16     </servlet-mapping>
17 </web-app>

springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7     http://www.springframework.org/schema/context        
 8     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9      
10     <context:component-scan base-package="controller" />
11     
12     <bean id="irViewResolver"
13         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="prefix" value="/WEB-INF/page/" />
15         <property name="suffix" value=".jsp" />
16     </bean>
17 
18 </beans>

 

运行结果:

 

 

运行成功!

 

posted @ 2021-01-21 13:42  影落潮生0  阅读(77)  评论(0编辑  收藏  举报