SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)

参考

(1)SpringMVC学习笔记(二)使用IntelliJ IDEA开发Spring MVC HelloWorld(基于Maven)(在这篇程序Demo代码的基础上演变)
(2)《Spring入门经典》(基于本书第三章)


代码结构

SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)_第1张图片

程序清单

AppConfig.java:

package com.wiley.beginningspring.ch3.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = {"com.wiley.beginningspring.ch3"})
public class AppConfig {

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

web.xml:



<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Applicationdisplay-name>
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextClassparam-name>
      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>
    init-param>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>com.wiley.beginningspring.ch3.config.AppConfigparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

PS:其它代码没有变

测试:

SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)_第2张图片

你可能感兴趣的:(Java__SpringMVC,Java__Spring)