Skip to content

Commit 65624a8

Browse files
committed
springmvc对restful的支持
1 parent ce5ddf7 commit 65624a8

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

WebRoot/WEB-INF/web.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,22 @@
5252
第三种:/*,这样配置是不对的,使用这种配置,最重要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址, 不能根据jsp页面找打Handler,会报错 -->
5353
<url-pattern>*.action</url-pattern>
5454
</servlet-mapping>
55+
56+
<!-- 配置springmvc的前端控制器DispatcherServlet,REST配置 -->
57+
<servlet>
58+
<servlet-name>springmvc_rest</servlet-name>
59+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
60+
61+
<!-- contextConfigLocation是配置springmvc加载的配置文件(配置处理器映射器、适配器等) 如果不配置contextConfigLocation,则默认加载的是/WEB-INF/springmvc-servlet.xml -->
62+
<init-param>
63+
<param-name>contextConfigLocation</param-name>
64+
<param-value>classpath:spring/springmvc.xml</param-value>
65+
</init-param>
66+
</servlet>
67+
68+
<servlet-mapping>
69+
<servlet-name>springmvc_rest</servlet-name>
70+
<url-pattern>/</url-pattern>
71+
</servlet-mapping>
5572

5673
</web-app>

WebRoot/jsonTest.jsp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ function requestJson(){
2424
alert(data.name);
2525
}
2626
27-
});
28-
27+
});
2928
3029
}
3130
//请求key/value,输出是json

config/spring/springmvc.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,8 @@
133133
<property name="maxUploadSize" value="5242880"/>
134134
<property name="defaultEncoding" value="utf-8"/>
135135
</bean>
136+
137+
<!-- 静态资源解析,包括js,css,img... -->
138+
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
139+
<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
136140
</beans>

src/ssm/controller/ItemsController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.validation.BindingResult;
1313
import org.springframework.validation.ObjectError;
1414
import org.springframework.validation.annotation.Validated;
15+
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
1617
import org.springframework.web.bind.annotation.RequestMapping;
1718
import org.springframework.web.bind.annotation.RequestParam;
@@ -196,5 +197,13 @@ public String editItemsQueryResult(ItemsQueryVo itemsQueryVo)
196197

197198
return itemsCustom; //由于@ResponseBody注解,将itemsCustom转成json格式返回
198199
}
200+
201+
//查询商品信息,输出json,使用RESTful
202+
@RequestMapping("/itemsView/{id}")
203+
public @ResponseBody ItemsCustom itemsView(@PathVariable("id") Integer id) throws Exception {
204+
ItemsCustom itemsCustom = itemsService.findItemsById(id);
205+
return itemsCustom;
206+
}
207+
199208

200209
}

0 commit comments

Comments
 (0)