springmvc的3中路径风格
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中路径风格的更多相关文章
- springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题
有两种解决方案: 1:在api路径中加入:.+ @RequestMapping("/findByIp/{ip:.+}") public Object test(@PathVaria ...
- springMvc中restful风格的api路径中把小数点当参数,SpringMvc中url有小数点
在springMvc web项目中restful风格的api路径中有小数点会被过滤后台拿不到最后一个小数点的问题, 有两种解决方案: 1:在api路径中加入:.+ @RequestMapping(&q ...
- SpringMVC(三)Restful风格及实例、参数的转换
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Restful风格 1.Restful风格的介绍 Restful 一种软件架构风格.设计风格,而不是 ...
- SpringMVC结合easyUI中datagird实现分页
SpringMVC结合easyUI中datagird实现分页 DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.轻量级,单元格合并.多列标题.冻结列和页脚只是其中 ...
- Java Web开发中路径问题小结
Java Web开发中,路径问题是个挺麻烦的问题,本文小结了几个常见的路径问题,希望能对各位读者有所帮助. (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 ...
- 11. java中路径/和\的区别
一般可以认为是"/"的作用等同于"\\"在java中路径一般用"/"windows中的路径一般用"\"linux.uni ...
- 解决SpringMVC拦截器中Request数据只能读取一次的问题
解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...
- quartusii开发过程中路径不能出现空格或中文
quartusii开发过程中路径不能出现空格或中文,否则软件出现.stf文件错误提示,开发环境搭建的时候也不能出现空格和中文,否则也会报错.
- Java Web 开发中路径相关问题小结
Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概 ...
随机推荐
- Iaas-cloudstack
http://cloudstack.apt-get.eu/systemvm/4.6/ 模板地址 http://cloudstack.apt-get.eu/centos7/4.6/ 代理及管理地址 ht ...
- linux工具之log4j-LogBack-slf4j-commons-logging
log4j http://commons.apache.org/proper/commons-logging/ http://logging.apache.org/log4j/2.x/ The Com ...
- messagepcak 资料
1,今天在hacknews上看到很多人对messagepack的争论.首先了解什么是MessagePack:MessagePack is a binary-based efficient object ...
- 计算机中丢失 msvcr110.dll 怎么办
笔者在一次运行 php.exe 时,运到“无法启动此程序,因为计算机中丢失 MSVCR110.dll.尝试重新安装该程序以解决此问题.”的提示,当时很无语,因为系统是刚刚安装好的,而且是最新版本的. ...
- 【转】Php+ajax+jsonp解决ajax跨域问题
首先:jsonp是json用来跨域的一个东西. 原理是通过script标签的跨域特性来绕过同源策略. 发送端: $.ajax({ type : "post", url : &quo ...
- DBA日常SQL之查询数据库运行状况
,) Day, ,),,)) H00, ,),,)) H01, ,),,)) H02, ,),,)) H03, ,),,)) H04, ,),,)) H05, ,),,)) H06, ,),,)) H ...
- 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册)
问题: 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册) 方法1: 我用的是64位操作系统.IIS中,启用32位应用程序设置为false.这样就可以了 ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(四)
http://www.cnblogs.com/StoneGarden/archive/2012/02/08/2343294.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- 剑指offer系列43---判断平衡二叉树
[题目]判断一颗二叉树是不是平衡二叉树. * 平衡二叉树定义:任意子节点深度相差不超过1.[思路]由上题,利用递归得到二叉树每个结点的深度同时比较. package com.exe9.offer; i ...
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
转自:http://blog.csdn.net/kalision/article/details/7692796 这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求 Timer类是用来执行任 ...