springmvc
先分析下代码,快速学习,先要把配置文件写好,
给上2个类具体看看
package com.shishuo.studio.action; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; 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 com.shishuo.studio.constant.SystemConstant; import com.shishuo.studio.entity.Category; import com.shishuo.studio.entity.vo.CourseVo; import com.shishuo.studio.entity.vo.PageVo; import com.shishuo.studio.exception.CategoryNotFoundException; import com.shishuo.studio.exception.notfound.StorageNotFoundException; import com.shishuo.studio.service.CategoryService; import com.shishuo.studio.service.UserService; /** * @author Herbert * */ @Controller @RequestMapping("/category") public class CategoryAction extends BaseAction { protected final Logger logger = Logger.getLogger(this.getClass()); @Autowired protected CategoryService categoryService; @Autowired protected UserService userService; /** * 首页 * * @param modelMap * @return */ @RequestMapping(value = "/{categoryId}.htm", method = RequestMethod.GET) public String category(@PathVariable long categoryId, ModelMap modelMap, @RequestParam(value = "p", defaultValue = "1") int p) { try { // 获得数据 Category category = categoryService.getCategoryById(categoryId); // 获取当前目录下的所有课程 PageVo<CourseVo> coursePageVo = courseService .getCoursePageByIdForUser(categoryId, p, 24); // 增加属性 modelMap.addAttribute("category", category); modelMap.put("coursePageVo", coursePageVo); return "category"; } catch (CategoryNotFoundException e) { return SystemConstant.PAGE_404; } catch (StorageNotFoundException e) { // TODO Auto-generated catch block return SystemConstant.PAGE_404; } } }
package com.shishuo.studio.action; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.shishuo.studio.action.auth.AuthBaseAction; @Controller @RequestMapping("/about") public class AboutAction extends AuthBaseAction { /** * 跳转到关于我们页面 * * @param modelMap * @param request * @return */ @RequestMapping(value = "/about.htm", method = RequestMethod.GET) public String about(ModelMap modelMap, HttpServletRequest request) { return "/about/about"; } /** * 跳转到服务协议页面 * * @param modelMap * @param request * @return */ @RequestMapping(value = "/service.htm", method = RequestMethod.GET) public String service(ModelMap modelMap, HttpServletRequest request) { return "/about/service"; } /** * 跳转到投诉举报页面 * * @param modelMap * @param request * @return */ @RequestMapping(value = "/complain.htm", method = RequestMethod.GET) public String complain(ModelMap modelMap, HttpServletRequest request) { return "/about/complain"; } /** * 跳转到版权声明页面 * * @param modelMap * @param request * @return */ @RequestMapping(value = "/copyright.htm", method = RequestMethod.GET) public String copyright(ModelMap modelMap, HttpServletRequest request) { return "/about/copyright"; } /** * 跳转到联系我们页面 * * @param modelMap * @param request * @return */ @RequestMapping(value = "/connect.htm", method = RequestMethod.GET) public String connect(ModelMap modelMap, HttpServletRequest request) { return "/about/connect"; } }
return "system/comment/comment";后面不需要东西
return "redirect:/admin/comment/page.htm";一般当我改变一个状态的时候 我需要还是显示在当前页面 就需要再进入Action 相当于再到数据库访问一次把 我改变的数据同个pageVo 显示到页面
spring的注解学习
@RequestParam("description") String description,
@PathVariable
请求路径上有个id的变量值,可以通过@PathVariable来获取 @RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
@autowired 自动配置 不需要写getter() setter()方法
@Deprecated 过时
@Repository 用在接口前面的类 比如ibits接口类的最前面
@ResponseBody当控制器返回页面不是字符串的时候 比如返回一个json对象用这个注解
@Controller控制器 加在控制器类的最前面
@RequestMapping("/admin/file")
放在类前面是这个路径下
@RequestMapping(value = "/index.htm", method = RequestMethod.GET)如果这个注解放在方法的前面 表示上面那个路径的基础下然后再是这个路劲
@RequestParam(value = "fileId", defaultValue = "1")当url传入参数的时候就可以拿到值
比如@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
public String update(
@RequestParam(value = "fileId", defaultValue = "1") long fileId,
ModelMap modelMap) throws Exception {}
相关配置文件如下
复制spring相关jar包到web-inf/lib里面
然后在web.xml加入
相当于springmvc的servlet
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping>