簡單的springmvc

controller:

package com.zking.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class Usercontroller {

    @RequestMapping(value = "demo01", method = RequestMethod.GET)
    public String demo01() {
        return "index";
    }

    //?是通配符可以輸入任何一個爲路徑如(1.action。。。。。a.cation)
    @RequestMapping(value = "/?")
    public String demo02() {

        return "index";
    }

    //*是通配符可以輸入任何多個爲路徑如(1123.action。。。。。abbbbb.cation)
    @RequestMapping(value = "/*")
    public String demo03() {
        return "index";
    }

    //**是通配符可以輸入任何一個爲路徑如(/1/2/3/4/5/5.action。。。。。/a/b/b/d/a/s/da/1.cation)
    @RequestMapping(value = "/**")
    public String demo04() {
        return "index";
    }

    //如果需要用PathVariable那麼web.xml裏面的url-path需要改爲/*
    @RequestMapping(value = "path/{id}")
    public String path(@PathVariable("id") int id) {
        return "index";
    }
    //如果需要用PathVariable那麼可以獲取getpost請求的參數如(a.action?id=10)
    @RequestMapping(value = "parm")
       public String  parm(@RequestParam("id") int id){
        System.out.println(id);
        return "index";
       }
    //如果不行寫?id=多少那麼可以用required = false,defaultValue = "0"
    @RequestMapping(value = "parm1")
    public String  parm1(@RequestParam(value = "id",required = false,defaultValue = "0") int id){
        System.out.println(id);
        return "index";
    }
}

spring-mvc.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
">
    <!--開啓springmvc掃描-->
    <context:component-scan base-package="com.zking.controller"/>
    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

web.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>
    <!--加載試圖解析器-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>


</web-app>

JAR:

  <!-- 引入spring-webmvc依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <!-- 引入jstl依賴 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- 引入servlet-api 依賴-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- 引入mybatis 依賴-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0</version>
        </dependency>
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章