springmvc的3中路径风格

1.导入相应的jar包,文件放置情况

springmvc的3中路径风格

2.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

 3   <display-name>springmvc7</display-name>

 4   <welcome-file-list>

 5     <welcome-file>index.jsp</welcome-file>

 6   </welcome-file-list>

 7   <servlet>

 8       <servlet-name>mvc-dispatcher</servlet-name>

 9       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

10       <!-- DispatcherServlet对应的上下文配置,默认/WEB-INF/$servlet-name$-servlet.xml

11           下面配置spring-mvc的核心配置文件

12        -->

13        <init-param>

14            <param-name>contextConfigLocation</param-name>

15            <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>

16        </init-param>

17       <load-on-startup>1</load-on-startup>

18   </servlet>

19   <servlet-mapping>

20       <servlet-name>mvc-dispatcher</servlet-name>

21       <!-- mvc-dispatcher 拦截所有的请求 -->

22       <url-pattern>/</url-pattern>

23   </servlet-mapping>

24   

25   

26 </web-app>

3.mvc-dispatcher-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans" 

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 4     xmlns:p="http://www.springframework.org/schema/p" 

 5     xmlns:context="http://www.springframework.org/schema/context" 

 6     xmlns:mvc="http://www.springframework.org/schema/mvc"

 7     xmlns:tx="http://www.springframework.org/schema/tx"

 8     xmlns:aop="http://www.springframework.org/schema/aop"

 9     xsi:schemaLocation="  

10         http://www.springframework.org/schema/beans 

11         http://www.springframework.org/schema/beans/spring-beans.xsd 

12         http://www.springframework.org/schema/context 

13         http://www.springframework.org/schema/context/spring-context.xsd 

14         http://www.springframework.org/schema/mvc

15         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        

16         http://www.springframework.org/schema/tx

17         http://www.springframework.org/schema/tx/spring-tx.xsd

18         http://www.springframework.org/schema/aop

19         http://www.springframework.org/schema/aop/spring-aop.xsd">

20 

21 <!-- 激活

22      @Required

23      @Autowired,jsr250's

24      @PostConstruct,

25      @PreDestroy and @ Resource等标注

26  -->

27 <context:annotation-config />

28 <!-- 

29     DispatcherServlet上下文,只搜索@Controller标注的类,不搜索其他标注的类

30  -->

31 <context:component-scan base-package="com.gys.mvcdemo">

32     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

33 </context:component-scan>

34 

35 <!-- 

36     

37     HandlerMapping无需配置,Spring MVC可以默认启动

38  -->

39  <!-- 

40      扩充了注解驱动,可以将请求参数绑定到控制器参数

41     启用基于annotation的handlerMapping.

42   -->

43 <mvc:annotation-driven />

44 

45 <!-- 

46     静态资源处理,css,js,imgs

47  -->

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

49 

50 

51 <!-- 视图解析器 -->

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

53     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

54     <property name="prefix" value="/WEB-INF/jsps/" />

55     <property name="suffix" value=".jsp" />

56 </bean>

57 

58 

59 </beans>

Course.java

 1 package com.gys.mvcdemo.model;

 2 

 3 public class Course {

 4     //课程IDd

 5     private Integer courseId;

 6     //课程名称

 7     private String title;

 8 //图片路径    

 9     private String imgPath;

10     //学习人数

11     private Integer learningNum;

12     //课程时长

13     private Long duration;

14     //课程难度

15     private Integer level;

16     //课程描述

17     private String levelDesc;

18     //课程介绍

19     private String descr;

20     public Integer getCourseId() {

21         return courseId;

22     }

23     public void setCourseId(Integer courseId) {

24         this.courseId = courseId;

25     }

26     public String getTitle() {

27         return title;

28     }

29     public void setTitle(String title) {

30         this.title = title;

31     }

32     public String getImgPath() {

33         return imgPath;

34     }

35     public void setImgPath(String imgPath) {

36         this.imgPath = imgPath;

37     }

38     public Integer getLearningNum() {

39         return learningNum;

40     }

41     public void setLearningNum(Integer learningNum) {

42         this.learningNum = learningNum;

43     }

44     public Long getDuration() {

45         return duration;

46     }

47     public void setDuration(Long duration) {

48         this.duration = duration;

49     }

50     public Integer getLevel() {

51         return level;

52     }

53     public void setLevel(Integer level) {

54         this.level = level;

55     }

56     public String getLevelDesc() {

57         return levelDesc;

58     }

59     public void setLevelDesc(String levelDesc) {

60         this.levelDesc = levelDesc;

61     }

62     public String getDescr() {

63         return descr;

64     }

65     public void setDescr(String descr) {

66         this.descr = descr;

67     }

68     

69     

70     

71     

72 }

CourseService.java

1 package com.gys.mvcdemo.service;

2 

3 import com.gys.mvcdemo.model.Course;

4 

5 public interface CourseService {

6     Course getCoursebyId(Integer courseId);

7 }

CourseServiceImpl.java

 1 package com.gys.mvcdemo.service.impl;

 2 

 3 import org.springframework.stereotype.Service;

 4 

 5 import com.gys.mvcdemo.model.Course;

 6 import com.gys.mvcdemo.service.CourseService;

 7 

 8 @Service("courseService")

 9 public class CourseServiceImpl implements CourseService{

10 

11     @Override

12     public Course getCoursebyId(Integer courseId) {

13         Course course=new Course();

14         course.setCourseId(courseId);

15         course.setTitle("深入浅出Java多线程");

16         course.setImgPath("1.jpg");

17         course.setLearningNum(123465);

18         course.setLevel(2);

19         course.setLevelDesc("中级");

20         course.setDuration(7200L);

21         course.setDescr("多线程是日常开发中的常用知识...........");

22         return course;

23     }

24 }

CourseController.java

 1 package com.gys.mvcdemo.controller;

 2 

 3 import java.util.Map;

 4 

 5 import javax.servlet.http.HttpServletRequest;

 6 import javax.servlet.http.HttpServletResponse;

 7 

 8 import org.springframework.beans.factory.annotation.Autowired;

 9 import org.springframework.stereotype.Controller;

10 import org.springframework.ui.Model;

11 import org.springframework.web.bind.annotation.PathVariable;

12 import org.springframework.web.bind.annotation.RequestMapping;

13 import org.springframework.web.bind.annotation.RequestMethod;

14 import org.springframework.web.bind.annotation.RequestParam;

15 

16 import com.gys.mvcdemo.model.Course;

17 import com.gys.mvcdemo.service.CourseService;

18 

19 @Controller

20 @RequestMapping("/courses")

21 public class CourseController {

22     

23     private CourseService courseService;

24 

25     @Autowired

26     public void setCourseService(CourseService courseService) {

27         this.courseService = courseService;

28     }

29     

30     // /courses/vies?courseId=123

31     @RequestMapping(value="/view",method=RequestMethod.GET)

32     public String viewCourse(Model model,@RequestParam("courseId") Integer courseId){

33         Course course=courseService.getCoursebyId(courseId);

34         model.addAttribute(course);

35         return "course_overview";

36     }

37     // courses/view2/{courseId}

38     @RequestMapping(value="/view2/{courseId}",method=RequestMethod.GET)

39     public String viewCourse2(@PathVariable("courseId") Integer courseId,Map<String, Object> model){

40         Course course=courseService.getCoursebyId(courseId);

41         model.put("course",course);

42         return "course_overview";

43     }

44     

45     // courses/view3?courseId=3

46     @RequestMapping("view3")

47     public String viewCourse3(HttpServletRequest request,HttpServletResponse response){

48         Integer courseId=Integer.valueOf(request.getParameter("courseId"));

49         Course course=courseService.getCoursebyId(courseId);

50         request.setAttribute("course", course);

51         return "course_overview";

52     }

53     

54     

55     

56 }

course_overview.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 2 <%

 3 String path = request.getContextPath();

 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 5 %>

 6 

 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 8 <html>

 9   <head>

10     <base href="<%=basePath%>">

11     

12     <title>My JSP 'course_overview.jsp' starting page</title>

13     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/main.css">

14   </head>

15   

16   <body>

17     <h2>id:${course.courseId }</h2>

18     <h2>title:${course.title }</h2>

19     <h2>imgPath:<img src="<%=path %>/resources/img/${course.imgPath }" /></h2>

20     <h2>learningNum:${course.learningNum }</h2>

21     <h2>duration:${course.duration }</h2>

22     <h2>level:${course.level }</h2>

23     <h2>levelDesc:${course.levelDesc }</h2>

24      <h2>descr:${course.descr }</h2>

25   </body>

26 </html>

用三种路径测试:

http://localhost:8080/springmvc7/courses/view?courseId=1

http://localhost:8080/springmvc7/courses/view2/2

http://localhost:8080/springmvc7/courses/view3?courseId=3

你可能感兴趣的:(springMVC)