SpringMvc 初步配置

spring-aop.jar
spring-bean.jar
spring-context.jar
spring-core.jar
spring-web.jar
spring-webmvc.jar
commons-logging.jar

springmvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 扫描 有注解的包 -->
    <context:component-scan base-package="handler"></context:component-scan>

    <!--配置视图解析器(InternalResourceViewResolver)  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
SpringMVCHandler
package handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//接口/类    注解   配置
@Controller
@RequestMapping(value = "handler") //映射
public class SpringMVCHandler {

    // @RequestMapping(value = "welcome")//映射
    // public String welcome() {
    //     return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    // }
    @RequestMapping(value = "welcome", method = RequestMethod.POST, params = {"name=zs", "age!=23", "!height"})//映射
    public String welcome() {
        return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    }


    @RequestMapping(value = "welcome2", headers = {"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding=gzip, deflate"})
    public String welcome2() {
        return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    }


    @RequestMapping(value = "welcome3/**/test")
    public String welcome3() {
        return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value = "welcome4/a?c/test")
    public String welcome4() {
        return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value = "welcome5/{name}")
    public String welcome5(@PathVariable("name") String name) {
        System.out.println(name);

        return "success";//  views/success.jsp,默认使用了 请求转发的 跳转方式
    }
}

 index.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>Insert title here</title>
</head>
<body>

<!-- 如果web.xml中的配置是
      <servlet-mapping>
              <servlet-name>springDispatcherServlet</servlet-name>
              <url-pattern>.action</url-pattern>
      </servlet-mapping>

    <%--<a href="user/welcome.action">first springmvc - welcome</a>    交由springmvc处理 找 @RuestMapping映射--%>
    <a href="user/welcome.action">first springmvc - welcome</a>交由springmvc处理  找 @RuestMapping映射
    <a href="xxx/welcome">first springmvc - welcome</a>            交由servlet处理  找url-parttern /@WebServlet()
 -->
<%--<a href="handler/welcome3/xyz/abcz/asb/test">33333333get - welcome</a>--%>
<%--<br/>--%>
<%--<a href="handler/welcome4/abc/test">4444444get - welcome</a>--%>
<%--<br/>--%>
<%--<a href="handler/welcome5/zs">555welcome</a>--%>

<form action="handler/welcome" method="post">
    name:<input name="name"><br/>
    age:<input name="age">
    height:<input name="height2">
    <input type="submit" value="post">
</form>

<a href="handler/welcome">first springmvc - welcome</a>

</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>welcome success</h1>

</body>
</html>

 

上一篇:C语言刷题组(基础),三


下一篇:spring boot整合模版引擎thymeleaf