Spring MVC —— 简单使用

在刚使用Spring MVC的时候,大部分的教程显示的最后结果都是jsp页面,并没有反馈json数据,Android开发过程中从服务器请求的是json/XML数据,如果不知道Spring MVC的组件的结构的话,可以看我上一篇文章Spring MVC —— 整体结构


显示jsp页面(此处代码参考传智.关云长代码)

1.新建Web工程

2.修改web.xml

<!-- 配置springmvc的前端控制器 DispatcherServlet-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 如果没有配置,默认是在/WEB-INF/'servletName'-servlet.xml -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-servlet.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>

3.根据web.xml中的配置,新建springmvc-servlet.xml

4.在Dispatcher Servlet的配置文件中配置处理器映射器与适配器
如果不在springmvc-servlet.xml中配置处理器映射器与适配器,表示使用默认的处理器映射器和适配器

<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.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

     <!-- 这个为控制器的包路径,表示Servlet启动后扫描该包 -->
    <context:component-scan base-package="myserver" />
</beans>

5.编写处理器Controller
该处使用的注解的方式

@Controller
public class ItemsController2{

    @RequestMapping("/queryItems")
    public ModelAndView queryItems(){

        List<Items> itemList = new ArrayList<Items>();
        Items items1 = new Items();
        items1.setName("联想笔记本");
        items1.setPrice(6000f);
        items1.setDetail("ThinkPad T430 联想笔记本电脑");
        itemList.add(items1);

        System.out.println(itemList.get(0).getName());
        Items items2 = new Items();
        items2.setName("苹果手机");
        items2.setPrice(5000f);
        items2.setDetail("iPhone6");

        itemList.add(items2);
        System.out.println(itemList.get(1).getName());

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("itemList", itemList);

        modelAndView.setViewName("/WEB-INF/items/itemsList.jsp");
        return modelAndView;
    }
}

6.配置ViewResolver

在springmvc-servlet.xml中添加

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

7.编写jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>查询商品列表</title>
    </head>

    <body>
        <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">

        商品列表:
            <table width="100%" border=0>
                <tr>
                    <td> 商品名称  </td>
                    <td> 商品价格  </td>
                    <td> 商品描述  </td>
                </tr>
                <c:forEach var="item" items="${itemList}">
                    <tr>
                            <td> ${item.name} </td>
                            <td> ${item.price} </td>
                            <td> ${item.detail} 1111</td>
                        </tr>
                </c:forEach>
            </table>
        </form>
    </body>
</html>

显示json数据(以前面的为基础添加)

显示json数据使用Spring MVC自带的 @ResponseBody

1、添加jar包(jackson-core-asl.jar、jackson-mapper-asl.jar)

2、在springmvc-servlet.xml中添加

<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.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="myserver" />
    <!-- 该处为添加语句 -->
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>

3、 在java文件中添加

    @RequestMapping("/xmlOrJson")
    @ResponseBody
    public Map<String, Object> xmlOrJson() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", "hello world");
        return map;
    }

注:在springmvc-servlet.xml中

<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.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

该链接哪些需要添哪些

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值