Notes the project to build Java SpringMVC

1.Eclipse New Dynamic Web Project project

 

2. lib WEB-INF file in the folder under WebContent introduced into the jar package, and Build Path

3. web.xml file in the WEB-INF under WebContent, front controller arranged SpringMVC

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
  <!-- 配置SpringMVC的前端控制器 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- 声明SpringMVC配置文件的位置 -->
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <!-- 所有以action结尾的请求都交给SpringMVC处理 -->
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <display-name>springmvc_demo</display-name>
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

New springmvc.xml file 4.src directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd " > 
    <! - one way: the way of non-comment -> 
    <! - will welcome.action request to com.springmvc.demo .controller.WelcomeController process -> 
    < the bean name = "/ welcome.action" class = "com.springmvc.demo.controller.WelcomeController" > </ the bean > 
    <-! configure the processor mapper -> 
    < the bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" > </ the bean > 
    <-! configure the processor adapter -> 
    <bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

5.src the new packet com.springmvc.demo.controller, this new class WelcomeController package, implemented Controller interface

package com.springmvc.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class WelcomeController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav=new ModelAndView();
        mav.addObject("message", "欢迎您:张三");
        mav.setViewName("welcome");
        return mav;
    }

}

6. jsp new file under the WEB-INF WebContent folder, the new folder in the file index.jsp and welcome.jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head> 
< Body > 
< h3 > Here is the index page </ h3 > 
< A href = "welcome.action" > Welcome page </ A > 
</ body > 
</ HTML >

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎页面</title>
</head>
<body>
<h1>${message }</h1>
</body>
</html>

7. Start the project, and then click index.js page "Welcome Page" link, do welcome.action request

8. Description

  1. In view springmvc.xml configured parser classes and two CI prefix and suffix, prefix is ​​a prefix configuration view, suffix is ​​a suffix configuration view.

  For example, the class name of the view in com.springmvc.demo.controller.WelcomeController ModelAndView object is set available for purchase, prefixes and suffixes view configuration view resolver spliced ​​into the real view /WEB-INF/jsp/welcome.jsp

  2. Use a non-annotated maps arranged processor and processor adapter, so called Handler (that is often said that the Controller class) to implement the Controller interface and override handleRequest method.

  This highlights a shortcoming: a Handler can only handle one request, request more, then defined Handler also more relevant request can only be placed in different Handler.

  So this has been annotated manner.

Guess you like

Origin www.cnblogs.com/antusheng/p/12651996.html