SpringMVC框架(2)之(2.5 Restful支持&配置静态资源过滤)

Restful支持

1. 什么是Restful:
Restful是软件开发的理念; HTTP进行了很好地诠释;

2. URL的Restful实现(较简洁):
Restful的URL:http: // localhost:8080/items/editItems / 1&…
正常的URL:http: // localhost:8080/items/editItems .action? id=1&…

需求:
根据 id查看商品信息,使用 Restful实现,返回 Json;
步骤:
1. DispatcherServlet的配置 url-pattern只能是“/”; (Restful时 web.xml中 url-pattern只能是“/” <url-pattern>/</url-pattern>,不能是*.action等其他);
2. 参数通过URL传递;
@RequestMapping中指定 Restful的 url参数,用 { }包起来:@RequestMapping("/viewItems/{id}/{name}")
@PathVariable将 url中 { }包起来的参数和后面跟的形参绑定:(@PathVariable(“id”) Integer id)
③ 若返回 json格式数据,在返回类型前加 @ResponseBody注解;

@RequestMapping("/viewItems/{id}/{name}")
public @ResponseBody ItemsCustom viewItems(@PathVariable("id") Integer id) throws Exception{
}

3. springmvc.xml 文件中配置静态资源过滤;

<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>

 

1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
         version="3.0">
         
         <!-- 1.配置DiapatcherServlet前端控制器 -->
         <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/springmvc.xml</param-value>
	        </init-param>
	        <load-on-startup>1</load-on-startup> 
         </servlet>
         <servlet-mapping>
         	<servlet-name>springmvc</servlet-name>
         	<url-pattern>*.action</url-pattern> 
         </servlet-mapping>

          
         <servlet>
         	<servlet-name>springmvc_restful</servlet-name>
         	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <init-param> 
	         	<param-name>contextConfigLocation</param-name>
	         	<param-value>classpath:spring/springmvc.xml</param-value>
	        </init-param>
	        <load-on-startup>1</load-on-startup> 
         </servlet>
         <servlet-mapping>
         	<servlet-name>springmvc_restful</servlet-name>
         	<!-- restful的配置方式 -->
         	<url-pattern>/</url-pattern> 
         </servlet-mapping>

         
         <!-- 2.加载启动Spring框架 -->
         <context-param> // 加载spring容器
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-*.xml</param-value>
         </context-param>
         <listener> // 加载监听
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>

         <welcome-file-list>         	
         </welcome-file-list>
</web-app>

2. ItemsController.java

@Controller
@RequestMapping("/items")
public class ItemsController{
	
    @Autowired
    private ItemsCustomMapper itemsCustomMapper;
    @Autowired
    private ItemsMapper itemsMapper;

    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception{
    	return itemsCustomMapper.findItemsList(itemsQueryVo);
    }
    
    //根据商品id查看商品信息restful接口
    //@RequestMapping中指定Restful的url的参数,用{}包起来
    @RequestMapping("/viewItems/{id}/{name}")
    public @ResponseBody ItemsCustom viewItems(@PathVariable("id") Integer id) throws Exception{
    	Items items=itemsMapper.selectByPrimaryKey(id);
    	return itemsCustom;
    }
}

3. ItemsList.jsp

<body>
    <table width="100%" border="1">
       <tr><td>商品名称</td><td>商品价格</td><td>订购日期</td><td>商品描述</td><td>操作</td></tr>
       <c:forEach items="${items}" var="item">
          <tr><td>${item.name}</td>
              <td>${item.price}</td>
              <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd"/></td>
              <td>${item.detail}</td>
              <td>
                <a href="${pageContext.request.contextPath}/items/editItems.action?id=${item.id}">修改</a>
              </td>

              <td>
                <a href="${pageContext.request.contextPath}/items/viewItems/${item.id}">查看商品</a>
              </td>

          </tr>
       </c:forEach>
    </table>
</body>

4. springmvc.xml
location 即文件 xx, mapping 被映射成 xx;)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

	<!-- 配置静态资源过滤 -->
	<mvc:resources mapping="/image/**" location="/image/"></mvc:resources> 
	<mvc:resources mapping="/css/**" location="/css/"></mvc:resources> 
	<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>

</beans>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值