Java笔记之SpringMVC(二):项目构建

1.Eclipse新建Dynamic Web Project项目

 

2.在WebContent下的WEB-INF下的lib文件夹中导入以下jar包,并Build Path

3.打开WebContent下的WEB-INF下的web.xml文件,配置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>

4.src目录下新建springmvc.xml文件

<?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">
    <!-- 方式一:非注解方式 -->
    <!-- 将welcome.action请求交给com.springmvc.demo.controller.WelcomeController处理 -->
    <bean name="/welcome.action" class="com.springmvc.demo.controller.WelcomeController"></bean>
    <!--配置处理器映射器 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    <!-- 配置处理器适配器 -->
    <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下新建包com.springmvc.demo.controller,在该包下新建WelcomeController类,实现Controller接口

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.在WebContent下的WEB-INF下新建jsp文件夹,在该文件夹下新建index.jsp和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>这里是index页面</h3>
<a href="welcome.action">欢迎页面</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.启动项目,之后点击index.js页面的"欢迎页面"的链接,做welcome.action请求

8.说明

  (1).在springmvc.xml中配置了视图解析器的类以及两个配置项prefix和suffix,prefix配置的是视图的前缀,suffix配置的是视图的后缀.

  比如,类com.springmvc.demo.controller.WelcomeController中ModelAndView对象中设置的视图名称为welcome,与视图解析器配置的视图前缀和后缀拼接成真正的视图/WEB-INF/jsp/welcome.jsp

  (2).使用非注解的方式配置处理器映射器和处理器适配器,这样的话调用的Handler(也就是常说的Controller类)要实现Controller接口,并重写handleRequest方法.

  这就突显出了一个缺点:一个Handler只能处理一个请求,请求多的话定义的Handler也就多了,相关的请求也只能放在不同的Handler中.

  所以这才有了注解的方式.

posted @ 2020-04-07 10:47  安徒生敲代码  阅读(237)  评论(0编辑  收藏  举报