SpringMVC注解开发基本配置

SpringMVC注解开发基本配置

配置文件

    • SpringMVC注解开发基本配置
  • pom.xml
  • web.xml
  • springmvc-servlet.xml
  • Controller
  • jsp

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.sean.mavengroupId>
  <artifactId>HelloartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  
  <dependencies>
  
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-webmvcartifactId>
	    <version>5.2.12.RELEASEversion>
	dependency>
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-aopartifactId>
	    <version>5.2.12.RELEASEversion>
	dependency>
	
	<dependency>
	    <groupId>aopalliancegroupId>
	    <artifactId>aopallianceartifactId>
	    <version>1.0version>
	dependency>
	
	<dependency>
	    <groupId>org.aspectjgroupId>
	    <artifactId>aspectjweaverartifactId>
	    <version>1.9.6version>
	dependency>
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-jdbcartifactId>
	    <version>5.2.12.RELEASEversion>
	dependency>
	
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-txartifactId>
	    <version>5.2.12.RELEASEversion>
	dependency>
  dependencies>
  

project>

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_4_0.xsd"
         version="4.0"
         metadata-complete="true">
  

  
<servlet>
    <servlet-name>springmvcservlet-name>
    
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
        
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc-servlet.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
    <servlet-name>springmvcservlet-name>
    
    <url-pattern>/url-pattern>
servlet-mapping>

web-app>

springmvc-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
 


<mvc:annotation-driven/>
<context:component-scan base-package="com.sean.ghs"/>
<mvc:default-servlet-handler/>

 
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
          
        <property name="prefix" value="/WEB-INF/jsp/"/>
        
        <property name="suffix" value=".jsp"/>
  bean>
  
    beans>

Controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	@RequestMapping("/hello")
	public String hello(Model model){
		model.addAttribute("msg","HelloSpringMVC");
		System.out.println(model.containsAttribute("msg"));
		return "hello";	
	}
}

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


     
  
  
  
    

${msg}

你可能感兴趣的:(springmvc)