SpringMVC - HelloWorld

SpringMVC - HelloWorld

SpringMVC 为表现层提供的基于 MVC 设计理念的优秀Web框架,目前主流的 MVC 框架之一

必备jar包:
       spring-core
       spring-context
       spring-spring-beans
       spring-aop
       spring-web
       spring-webmvc

配置 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">
    <display-name>Archetype Created Web Application</display-name>
    <!--welcome pages-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!--配置springmvc DispatcherServlet-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--配置dispatcher-servlet.xml作为mvc的配置文件-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--把applicationContext.xml加入到配置文件中-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>springUtf8Encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springUtf8Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

创建 HelloWorld 类,并使用注解来请求 URL

@RequestMapping("/springmvc")
@Controller
public class HelloWorld {
    private static final String SUCCESS = "success";
    //使用 RequestMapping 注解来映射为post请求的 URL 
    @RequestMapping(value = "/helloworld",method = RequestMethod.POST)
    public String hello(){
        System.out.println("Hello World");
        return SUCCESS;
    }
}

配置 SpringMVC 的配置文件

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.exer.springmvc"></context:component-scan>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--请求将转发到 /WEB-INF/views/success.jsp 下-->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

SpringMVC 会按请求参数名和 pojo 属性名自动进行匹配,自动为该对象填充属性。

       创建 Address 类,并加入 GET & SET 方法

    private String province;
    private String city;

       创建 User 类,与Address 关联,并加入 GET & SET 方法

private String username;
    private String password;
    private int age;
    private Address address;

在 jsp 页面 加入对应表单

	<form action="springmvc/testPogo" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>
        age:<input type="text" name="age"><br>
        province:<input type="text" name="Address.province"><br>
        city:<input type="text" name="Address.city"><br>
        <input type="submit" value="提交">
    </form>
@RequestMapping("/springmvc")
@Controller
public class HelloWorld {
    private static final String SUCCESS = "success";

    @RequestMapping("/testPogo")
    public String testPojo(User user){
        System.out.println("testPojo:" + user);
        return SUCCESS;
    }
}

你可能感兴趣的:(SpringMVC)