码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC学习记录

时间:2020-08-12 15:40:55      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:spl   ext   xmlns   let   ble   span   info   新建   ppi   

SpringMVC学习记录

1、HelloWorld

  1、新建web工程,添加jar包

技术图片

 

 

 

  2、配置web.xml配置springmvc核心控制器(DispatcherServlet):作用是加载springmvc的配置文件,核心控制器自动加载默认位置配置文件,默认位置为web-inf下,默认名称为***-servlet.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--配置核心控制器-->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

 

springMVC-servlet.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描组件,将加上@Controller的类作为控制层-->
    <context:component-scan base-package="com.zhaojianhui"></context:component-scan>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

 

3、创建一个pojo

@Controller
public class Test {
    @RequestMapping(value = "hello",method = RequestMethod.GET)//value设置请求路径,method用于约束请求类型
    public String hello() {
        System.out.println("111");
        return "success";
    }
}

  

4、编写jsp测试代码

<%@ page language="java" pageEncoding="UTF-8" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="hello">测试springmvc</a>
</body>
</html>

5、测试post请求

@RequestMapping(value = "hello",method = RequestMethod.POST)
    public String hello1() {
        System.out.println("POST");
        return "success";
    }

  

<form action="hello" method="post">
    <input type="submit" value="提交"/>
</form>

 

 

@RequestMapping解析:

技术图片

 

 

 技术图片

 


 

 

 技术图片

 

 

 


 

占位符与占位符里值的获取示例(@pathvariable注解):

//占位符
    @RequestMapping(value = "/test/{id}/{name}")
    public String hello2(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        System.out.println("id" + id + ",name:" + name);
        return "success";
    }

  

SpringMVC学习记录

标签:spl   ext   xmlns   let   ble   span   info   新建   ppi   

原文地址:https://www.cnblogs.com/zhaojianhui/p/13488284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!