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

2.web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>springmvc7</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置,默认/WEB-INF/$servlet-name$-servlet.xml
下面配置spring-mvc的核心配置文件
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- mvc-dispatcher 拦截所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

3.mvc-dispatcher-servlet.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.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"> <!-- 激活
@Required
@Autowired,jsr250's
@PostConstruct,
@PreDestroy and @ Resource等标注
-->
<context:annotation-config />
<!--
DispatcherServlet上下文,只搜索@Controller标注的类,不搜索其他标注的类
-->
<context:component-scan base-package="com.gys.mvcdemo">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!-- HandlerMapping无需配置,Spring MVC可以默认启动
-->
<!--
扩充了注解驱动,可以将请求参数绑定到控制器参数
启用基于annotation的handlerMapping.
-->
<mvc:annotation-driven /> <!--
静态资源处理,css,js,imgs
-->
<mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

Course.java

 package com.gys.mvcdemo.model;

 public class Course {
//课程IDd
private Integer courseId;
//课程名称
private String title;
//图片路径
private String imgPath;
//学习人数
private Integer learningNum;
//课程时长
private Long duration;
//课程难度
private Integer level;
//课程描述
private String levelDesc;
//课程介绍
private String descr;
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public Integer getLearningNum() {
return learningNum;
}
public void setLearningNum(Integer learningNum) {
this.learningNum = learningNum;
}
public Long getDuration() {
return duration;
}
public void setDuration(Long duration) {
this.duration = duration;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public String getLevelDesc() {
return levelDesc;
}
public void setLevelDesc(String levelDesc) {
this.levelDesc = levelDesc;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
} }

CourseService.java

 package com.gys.mvcdemo.service;

 import com.gys.mvcdemo.model.Course;

 public interface CourseService {
Course getCoursebyId(Integer courseId);
}

CourseServiceImpl.java

 package com.gys.mvcdemo.service.impl;

 import org.springframework.stereotype.Service;

 import com.gys.mvcdemo.model.Course;
import com.gys.mvcdemo.service.CourseService; @Service("courseService")
public class CourseServiceImpl implements CourseService{ @Override
public Course getCoursebyId(Integer courseId) {
Course course=new Course();
course.setCourseId(courseId);
course.setTitle("深入浅出Java多线程");
course.setImgPath("1.jpg");
course.setLearningNum(123465);
course.setLevel(2);
course.setLevelDesc("中级");
course.setDuration(7200L);
course.setDescr("多线程是日常开发中的常用知识...........");
return course;
}
}

CourseController.java

 package com.gys.mvcdemo.controller;

 import java.util.Map;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.gys.mvcdemo.model.Course;
import com.gys.mvcdemo.service.CourseService; @Controller
@RequestMapping("/courses")
public class CourseController { private CourseService courseService; @Autowired
public void setCourseService(CourseService courseService) {
this.courseService = courseService;
} // /courses/vies?courseId=123
@RequestMapping(value="/view",method=RequestMethod.GET)
public String viewCourse(Model model,@RequestParam("courseId") Integer courseId){
Course course=courseService.getCoursebyId(courseId);
model.addAttribute(course);
return "course_overview";
}
// courses/view2/{courseId}
@RequestMapping(value="/view2/{courseId}",method=RequestMethod.GET)
public String viewCourse2(@PathVariable("courseId") Integer courseId,Map<String, Object> model){
Course course=courseService.getCoursebyId(courseId);
model.put("course",course);
return "course_overview";
} // courses/view3?courseId=3
@RequestMapping("view3")
public String viewCourse3(HttpServletRequest request,HttpServletResponse response){
Integer courseId=Integer.valueOf(request.getParameter("courseId"));
Course course=courseService.getCoursebyId(courseId);
request.setAttribute("course", course);
return "course_overview";
} }

course_overview.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'course_overview.jsp' starting page</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/main.css">
</head> <body>
<h2>id:${course.courseId }</h2>
<h2>title:${course.title }</h2>
<h2>imgPath:<img src="<%=path %>/resources/img/${course.imgPath }" /></h2>
<h2>learningNum:${course.learningNum }</h2>
<h2>duration:${course.duration }</h2>
<h2>level:${course.level }</h2>
<h2>levelDesc:${course.levelDesc }</h2>
<h2>descr:${course.descr }</h2>
</body>
</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的3中路径风格的更多相关文章

  1. springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题

    有两种解决方案: 1:在api路径中加入:.+ @RequestMapping("/findByIp/{ip:.+}") public Object test(@PathVaria ...

  2. springMvc中restful风格的api路径中把小数点当参数,SpringMvc中url有小数点

    在springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题, 有两种解决方案: 1:在api路径中加入:.+ @RequestMapping(&q ...

  3. SpringMVC(三)Restful风格及实例、参数的转换

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Restful风格 1.Restful风格的介绍 Restful 一种软件架构风格.设计风格,而不是 ...

  4. SpringMVC结合easyUI中datagird实现分页

    SpringMVC结合easyUI中datagird实现分页 DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.轻量级,单元格合并.多列标题.冻结列和页脚只是其中 ...

  5. Java Web开发中路径问题小结

     Java Web开发中,路径问题是个挺麻烦的问题,本文小结了几个常见的路径问题,希望能对各位读者有所帮助. (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 ...

  6. 11. java中路径/和\的区别

    一般可以认为是"/"的作用等同于"\\"在java中路径一般用"/"windows中的路径一般用"\"linux.uni ...

  7. 解决SpringMVC拦截器中Request数据只能读取一次的问题

    解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...

  8. quartusii开发过程中路径不能出现空格或中文

    quartusii开发过程中路径不能出现空格或中文,否则软件出现.stf文件错误提示,开发环境搭建的时候也不能出现空格和中文,否则也会报错.

  9. Java Web 开发中路径相关问题小结

    Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概 ...

随机推荐

  1. Android中用双缓存技术,加载网络图片

    最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...

  2. spring基于注解的配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. windows中用eclipse开发Android环境搭建SDK安装异常

    SDK更新时的“https://dl-ssl.google.com refused”错误 Download interrupted: hostname in certificate didn't ma ...

  4. PIP安装Python的scipy,scrapy等包出现“failed building wheel for xxx”问题解决办法

    1.在这里下载对应的.whl文件,注意别改文件名! http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml Ctrl + F,输入lxml,找到下面这段 Lxm ...

  5. net cookie操作

    写入方法: HttpCookie cookie = new HttpCookie("id"); cookie.Value=cookieValue; cookie.Expires = ...

  6. 图解SQL的各种连接join[转]

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  7. 【linux】rpm常见命令

    查看包依赖模块:http://www.rpmfind.net/ rpm  -ivh [包名] :安装rpm包 rpm  -e [包名] :删除rpm包 rpm  -aq [包名] :查询rpm包 rp ...

  8. 剑指offer系列44---只出现一次 的数字

    [题目]一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. * [思路]异或性质:数异或自己即为0: * 一个数组中,从头到尾异或的结果为不重复数字异或结果. ...

  9. 【jdbcTemplate】使用jdbcTemplate查询的三种回调

    用于查询的回调接口定义主要有以下三种: org.springframework.jdbc.core.ResultSetExtractor.  基本上属于JdbcTemplate内部使用的Callbac ...

  10. (二)java特征

    java的核心是面向对象,与之相对的是面向过程的编程,在对整个java编程没有足够的理解和运用的情况下恐怕没办法很好的理解这两个概念.     在我的初步理解中,写一个程序就例如做一件事情,面向过程的 ...