JavaWeb项目入门

SpringMVC项目入门(Maven)

源码请见共享目录CSDN/Java Web(Maven)/1.SpringMVC项目入门

一、目标

  • 自定义控制器(controller)
  • 使用json解析(FastJson&Jackson)

二、工程结构

新建一个maven-archetype-webapp工程,增加代码,项目结构如下:
这里写图片描述

三、导入jar文件

在pom.xml增加如下spring-webmvc的依赖,spring-webmvc底层依赖的jar可不填,maven会通过网络去找到spring-aop、spring-beans等jar。

pom.xml 中部分代码
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
    </dependencies>

这里写图片描述

四、代码

4.1、自定义控制器

第一步:在工程的src/main/java目录下的cn.dectfix.controller包中新建Test.java,代码如下:

package cn.dectfix.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Test {
    @RequestMapping("foo1")
    @ResponseBody
    public String foo1(){
        System.out.println("Hello World1!");
        return "Hello";
    }
    @RequestMapping("foo2")
    @ResponseBody
    public String foo2(){
        System.out.println("Hello World2!");
        return "Hello";
    }
}
  • 使用@Controller注解注释该类是控制器类;
  • 使用@RequestMapping注解指定每个方法的访问URL,这里我定义了http://127.0.0.1/foo1http://127.0.0.1/foo1foo2两个控制器。
  • 使用@ResponseBody注解,指定控制器将内容或对象作为 HTTP 响应正文返回。

第二步:在工程的src/main/resources目录下,新建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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.0.xsd   
            http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
    <!-- 扫描actions组件 -->
    <context:component-scan base-package="cn.dectfix.controller"></context:component-scan>
</beans>    

spring-mvc.xml文件中,context:component-scan标签是用于扫描出被@Controller/@Service等注解注释的类。这里我们要写控制器,会用到@Controller注解。

第三步:配置web.xml文件,增加Spring MVC的DispatcherServlet,用于拦截带斜杠的URL,同时也可以增加拦截带*.js等的请求。(注意web.xml中contextConfigLocation参数的值是第一步中的文件位置

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>
    <!-- 配置springmvc -->
    <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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
</web-app>

第四步:启动工程,并访问http://127.0.0.1/foo1,可以看到控制台和网页显示出的信息如下图:
这里写图片描述

4.2、json解析

在4.1节中定义的如public String foo1(){…}返回值为String类型的控制器,能够正常的返回字符串,那么能不能返回整型、浮点、布尔、引用等类型呢?答案是需要用json解析!

继续在4.1的基础上增加User.java和Test2.java的代码!

package cn.dectfix.controller;

public class User {
    private String username;
    private String password;
    private int uid;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
}
package cn.dectfix.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Test2 {
    @RequestMapping("test1")
    @ResponseBody
    public int helo1(){
        return 1;
    }
    @RequestMapping("test2")
    @ResponseBody
    public boolean helo2(){
        return true;
    }
    @RequestMapping("test3")
    @ResponseBody
    public User helo3(){
        User u = new User();
        u.setUid(10010);
        u.setUsername("memorylorry");
        u.setPassword("123456");
        return u;
    }
}

写完后,允许起服务器,发现访问test1、test2、test3这些控制器,都显示资源类型不接受,那这里需要用到json解析,下面分FaskJson和Jackson解析来说明。
这里写图片描述

4.2.1 方法1-使用FastJson解析

在pom.xml中增加依赖(fastjson内部用到了jackson):

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.29</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.5</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.5</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.5</version>
</dependency> 

在spring-mvc.xml文件中,增加FastJson插件,发现遇到错误了,说mvc:annotation-drivern中的children项是空的。
这里写图片描述

修改beans标签的xsi:schemaLocation的xsd的版本都改为4.3问题得以解决,最后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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.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   
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
    <!-- 扫描actions组件 -->
    <context:component-scan base-package="cn.dectfix.conntroller"></context:component-scan>

        <mvc:annotation-driven>

        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                        <value>application/x-www-form-urlencoded;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

4.2.2 方法2-使用Jackson解析

在pom.xml中增加Jackson的依赖:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.5</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.5</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.5</version>
    </dependency> 

在spring-mvc.xml文件中,增加Jackson插件,spring-mvc.xml改成了如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    ">
    <context:component-scan base-package="cn.dectfix.controller" />
    <context:component-scan base-package="cn.dectfix.dto" />
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                        <value>application/x-www-form-urlencoded;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

在完成4.2.1或4.2.2节的json解析配置后,访问test3地址,会直接向页面输出如下内容。

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MEMORYLORRY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值