springMVC入门一

2021-06-30 11:06

阅读:345

标签:root   prefix   nal   图片   library   display   return   html4   pre   

一、准备工作

eclipse使用maven搭建项目,项目名称为HelloSpringMVC

二、搭建好的项目如下:

技术分享图片

项目介绍:实现简单的helloworld

三、具体代码

controller类:HelloWorldController

package com.cn.springmvc;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")   //访问http://localhost:8080/HelloSpringMVC/hello 
    public String hello(Model model){
        model.addAttribute("greeting","hello Spring MVC");  //jsp页面接收 ${greeting}
        
        return "helloworld";  //跳转到 helloworld.jsp
    }
}

jsp页面:helloworld.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>Spring4 MVC -HelloWorldtitle>
head>
body>
    h1>${greeting}h1>
body>
html>

web项目配置:web.xml

xml version="1.0" encoding="UTF-8"?>
web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    display-name>HelloSpringMVCdisplay-name>

    servlet>
        
        servlet-name>spring-mvcservlet-name>
        servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        
        
        
        
        load-on-startup>1load-on-startup>
    servlet>
    
    
    servlet-mapping>
        servlet-name>spring-mvcservlet-name>
        url-pattern>/url-pattern>
    servlet-mapping>

    
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>/WEB-INF/root-context.xmlparam-value>
    context-param>

    
    listener>
        listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

web-app>

springMVC配置:spring-mvc-servlet.xml,root-context.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:p="http://www.springframework.org/schema/p"
    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.1.xsd 
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.1.xsd 
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    
    context:component-scan base-package="com.cn.springmvc"/>

    
    mvc:default-servlet-handler />
    

    
    
    mvc:annotation-driven/>
    

    
    bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        property name="prefix" value="/pages/" />  
        property name="suffix" value=".jsp" />     
    bean>
beans>

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/maven-v4_0_0.xsd">
  modelVersion>4.0.0modelVersion>
  groupId>com.cngroupId>
  artifactId>HelloSpringMVCartifactId>
  packaging>warpackaging>
  version>0.0.1-SNAPSHOTversion>
  name>HelloSpringMVC Maven Webappname>
  url>http://maven.apache.orgurl>
  dependencies>
    dependency>
      groupId>junitgroupId>
      artifactId>junitartifactId>
      version>3.8.1version>
      scope>testscope>
    dependency>
    
    
    
    dependency>
        groupId>javax.servletgroupId>
        artifactId>javax.servlet-apiartifactId>
        version>3.1.0version>
        scope>providedscope>
    dependency>
    
    
    
    dependency>
        groupId>org.springframeworkgroupId>
        artifactId>spring-coreartifactId>
        version>4.1.4.RELEASEversion>
    dependency>    
    
    
    dependency>
        groupId>org.springframeworkgroupId>
        artifactId>spring-webartifactId>
        version>4.1.4.RELEASEversion>
    dependency>

    
    dependency>
        groupId>org.springframeworkgroupId>
        artifactId>spring-webmvcartifactId>
        version>4.1.4.RELEASEversion>
    dependency>
    
  dependencies>
  build>
    finalName>HelloSpringMVCfinalName>
    
  build>
project>

四、访问:http://localhost:8080/HelloSpringMVC/hello

 技术分享图片

 

 

 

 

  

 

springMVC入门一

标签:root   prefix   nal   图片   library   display   return   html4   pre   

原文地址:https://www.cnblogs.com/x-jingxin/p/9641940.html


评论


亲,登录后才可以留言!