@Controller 和 @RequestMapping 进行spring mvc 映射
程序员文章站
2024-03-24 08:20:22
...
1、applicationContext-service.xml
2、rest-servlet.xml
3、controller-servlet.xml
4、AppPageController.java
采用spring mvc 注解方式来进行映射。
<bean id="appPageService" class="com.xiaonei.wap.fuxi.xoa.client.AppPageServiceDelegate" />
2、rest-servlet.xml
<!-- Default View Resolver -->
<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" >
<property name="order" value="0"/>
</bean>
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- url mapping by bean name -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0" />
</bean>
3、controller-servlet.xml
<bean class="com.renren.wap.fuxi.controllers.AppPageController">
<property name="appPageService" ref="appPageService" />
<property name="appPageCategoryService" ref="appPageCategoryService" />
<property name="userPlayService" ref="userPlayService" />
<property name="userFacade" ref="userFacade" />
</bean>
4、AppPageController.java
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.renren.wap.fuxi.controllers;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.renren.wap.fuxi.constants.FuxiConstants;
import com.xiaonei.platform.core.model.User;
import com.xiaonei.platform.core.model.WUserCache;
import com.xiaonei.wap.framework.user.facade.IUserFacade;
import com.xiaonei.wap.fuxi.model.AppPage;
import com.xiaonei.wap.fuxi.model.AppPageResult;
import com.xiaonei.wap.fuxi.model.AppPageSnapshot;
import com.xiaonei.wap.fuxi.model.AppPageSupport;
import com.xiaonei.wap.fuxi.model.UserPlayFlowResult;
import com.xiaonei.wap.fuxi.service.IAppPageCategoryService;
import com.xiaonei.wap.fuxi.service.IAppPageService;
import com.xiaonei.wap.fuxi.service.IUserPlayService;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-15下午05:26:11
*/
@Controller
@RequestMapping("/page")
public class AppPageController {
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(AppPageController.class);
private IAppPageService appPageService;
private IAppPageCategoryService appPageCategoryService;
private IUserPlayService userPlayService;
private IUserFacade userFacade;
@RequestMapping(value = "/{pageId}", method = RequestMethod.GET)
public ModelAndView page(
HttpServletRequest request,
User host,
@PathVariable("pageId") int pageId,
@RequestParam(value = "os", required = false, defaultValue = FuxiConstants.OS_VALUE) int os) {
ModelAndView mav = new ModelAndView("pages/wap/apppage");
AppPage appPage = appPageService.getPage(pageId);
if (appPage == null) {
return null;
}
String downloadUrl = null;
List<AppPageSupport> appPageSupportList = appPage.getAppPageSupports();
if (!CollectionUtils.isEmpty(appPageSupportList)) {
for (AppPageSupport appPageSupport : appPageSupportList) {
if (os == appPageSupport.getOs()) {
downloadUrl = appPageSupport.getWapDownloadUrl();
break;
}
}
}
if (downloadUrl != null) {
mav.addObject("downloadUrl", downloadUrl);
}
List<AppPageSnapshot> snapshot = null;
if (appPage.getWapSnapshots() != null) {
snapshot = appPage.getWapSnapshots();
}
mav.addObject("snapshot", snapshot);
List<Integer> friendIdList = userPlayService.getFriendPlayByPageId(host.getId(), pageId,
FuxiConstants.EXPIRED_DAYS);
if (!CollectionUtils.isEmpty(friendIdList)) {
String friends = getFriendsNameAndCount(friendIdList);
appPage.setFriends(friends);
}
mav.addObject("appPage", appPage);
return mav;
}
@RequestMapping(value = "/hot/list", method = RequestMethod.GET)
public ModelAndView hotPages(
HttpServletRequest request,
User host,
@RequestParam(value = "os", required = false, defaultValue = FuxiConstants.OS_VALUE) int os) {
ModelAndView mav = new ModelAndView("pages/wap/apppageshot");
AppPageResult topList = appPageService.getTopPageList(os, 0, FuxiConstants.HOT_SIZE_VALUE);
if (topList != null) {
mav.addObject("topList", topList);
}
AppPageResult gameList = getCategory(os, FuxiConstants.APP_PAGE_CATEGORY_GAME);
if (gameList != null) {
buildAppPageList(host.getId(), gameList);
mav.addObject("gameList", gameList);
}
AppPageResult appList = getCategory(os, FuxiConstants.APP_PAGE_CATEGORY_APP);
if (appList != null) {
buildAppPageList(host.getId(), appList);
mav.addObject("appList", appList);
}
AppPageResult webList = getCategory(os, FuxiConstants.APP_PAGE_CATEGORY_WEB);
if (webList != null) {
buildAppPageList(host.getId(), webList);
mav.addObject("webList", webList);
}
mav.addObject("os", os);
return mav;
}
@RequestMapping("/search")
public ModelAndView search(
HttpServletRequest request,
User host,
@RequestParam(value = "keyword", required = false, defaultValue = "") String keyword,
@RequestParam(value = "kw", required = false, defaultValue = "") String kw,
@RequestParam(value = "os", required = false, defaultValue = FuxiConstants.OS_VALUE) int os,
@RequestParam(value = "page", required = false, defaultValue = FuxiConstants.PAGE_VALUE) int page,
@RequestParam(value = "pageSize", required = false, defaultValue = FuxiConstants.PAGE_SIZE_VALUE) int pageSize) {
ModelAndView mav = new ModelAndView("pages/wap/apppagessearch");
mav.addObject("keyword", keyword);
AppPageResult appPageResult = appPageService.searchPageList(keyword, os, page, pageSize);
if (appPageResult != null) {
buildAppPageList(host.getId(), appPageResult);
mav.addObject(FuxiConstants.LIST, appPageResult);
mav.addObject(FuxiConstants.COUNT, appPageResult.getCount());
}
mav.addObject(FuxiConstants.PAGE_VALUE, page);
mav.addObject(FuxiConstants.PAGE_SIZE_VALUE, pageSize);
mav.addObject("os", os);
return mav;
}
@RequestMapping(value = "/category/{categoryId}", method = RequestMethod.GET)
public ModelAndView category(
HttpServletRequest request,
User host,
@PathVariable("categoryId") int categoryId,
@RequestParam(value = "os", required = false, defaultValue = FuxiConstants.OS_VALUE) int os,
@RequestParam(value = "page", required = false, defaultValue = FuxiConstants.PAGE_VALUE) int page,
@RequestParam(value = "pageSize", required = false, defaultValue = FuxiConstants.PAGE_SIZE_VALUE) int pageSize) {
ModelAndView mav = new ModelAndView("pages/wap/apppagescategory");
AppPageResult appPageResult = appPageCategoryService.getCategoryPageList(categoryId, os,
FuxiConstants.APP_PAGE_SORT_TYPE_DISPLAY, page, pageSize);
if (appPageResult == null) {
if (logger.isDebugEnabled()) {
logger.debug("AppPageResult is null");
}
return null;
}
buildAppPageList(host.getId(), appPageResult);
mav.addObject(FuxiConstants.LIST, appPageResult);
mav.addObject("categoryId", categoryId);
mav.addObject("os", os);
mav.addObject(FuxiConstants.PAGE_VALUE, page);
mav.addObject(FuxiConstants.COUNT, appPageResult.getCount());
mav.addObject(FuxiConstants.PAGE_SIZE_VALUE, pageSize);
return mav;
}
/**
* @param os
* @param categoryId
* @return
*/
private AppPageResult getCategory(int os, int categoryId) {
AppPageResult categoryList = appPageCategoryService.getCategoryPageList(categoryId, os,
FuxiConstants.APP_PAGE_SORT_TYPE_TIME, 0, 2);
if (categoryList == null) {
return null;
}
return categoryList;
}
/**
* 根据一组friendIds拼接friends展示描述
*
* @param friendIds
* @param count
* @return
*/
private String getFriendsNameAndCount(List<Integer> friendIdList) {
StringBuilder friendNames = new StringBuilder();
if (!CollectionUtils.isEmpty(friendIdList)) {
int friendsSize = friendIdList.size();
if (friendIdList.size() > 2) {
friendIdList = friendIdList.subList(0, 2);
}
List<WUserCache> users = userFacade.getUsersCacheList(friendIdList);
if (users != null) {
for (Iterator<WUserCache> iterator = users.iterator(); iterator.hasNext();) {
WUserCache wUserCache = iterator.next();
friendNames.append(wUserCache.getName());
if (iterator.hasNext()) {
friendNames.append(",");
}
}
friendNames.append("等").append(friendsSize).append("个好友在玩");
}
}
if (friendNames.length() == 0) {
friendNames.append("暂时没有好友在玩");
}
return friendNames.toString();
}
/**
* 根据userId, appPageResult包装page展示列表
*
* @param userId
* @param appPageResult
*/
private void buildAppPageList(int userId, AppPageResult appPageResult) {
if (appPageResult != null && appPageResult.getCount() > 0) {
List<Integer> pageIdList = new ArrayList<Integer>();
for (AppPage appPage : appPageResult.getAppPageList()) {
pageIdList.add(appPage.getId());
}
List<UserPlayFlowResult> friendIdList = null;
if (!CollectionUtils.isEmpty(pageIdList)) {
friendIdList = userPlayService.getFriendPlayByPageIds(userId, pageIdList,
FuxiConstants.EXPIRED_DAYS);
}
if (!CollectionUtils.isEmpty(friendIdList)) {
for (AppPage appPage : appPageResult.getAppPageList()) {
int pageId = appPage.getId();
for (Iterator<UserPlayFlowResult> iterator = friendIdList.iterator(); iterator
.hasNext();) {
UserPlayFlowResult userPlayFlowResult = (UserPlayFlowResult) iterator
.next();
if (pageId == userPlayFlowResult.getPageId()
&& userPlayFlowResult.getFriends() != null) {
int friendCount = userPlayFlowResult.getFriends().size();
if (friendCount > 0) {
String friends = getFriendsNameAndCount(userPlayFlowResult
.getFriends());
appPage.setFriends(friends);
}
}
}
}
}
}
}
public void setAppPageService(IAppPageService appPageService) {
this.appPageService = appPageService;
}
public void setAppPageCategoryService(IAppPageCategoryService appPageCategoryService) {
this.appPageCategoryService = appPageCategoryService;
}
public void setUserPlayService(IUserPlayService userPlayService) {
this.userPlayService = userPlayService;
}
public void setUserFacade(IUserFacade userFacade) {
this.userFacade = userFacade;
}
}
采用spring mvc 注解方式来进行映射。
推荐阅读
-
Spring MVC之Controller RequestMapping
-
@Controller 和 @RequestMapping 进行spring mvc 映射
-
Spring中Controller和RequestMapping的详解
-
spring-mvc/springboot使用MockMvc对controller进行测试
-
详解获取Spring MVC中所有RequestMapping以及对应方法和参数
-
详解获取Spring MVC中所有RequestMapping以及对应方法和参数
-
spring-mvc/springboot使用MockMvc对controller进行测试
-
spring mvc 获取所有的controller和url映射关系
-
Spring MVC温故而知新系列教程之请求映射RequestMapping注解
-
Spring MVC温故而知新系列教程之请求映射RequestMapping注解