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 那么针对这个站点的几个基本概 ...
随机推荐
- Xilium.CefGlue怎么使用Js调用C#方法
第1篇:.NET多种WebKit内核/Blink内核浏览器初步测评报告http://www.wuleba.com/?p=23590 第2篇:Xilium CefGlue 关于 CLR Object 与 ...
- 通过命令行安装Android app
手动安装安卓app的命令为:adb install -r C:\Users\Lihao\workspace\Appium_Demo\apps\app_F_1.3.0.apk
- asp.net生成PDF文件 (1)
asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- nginx path_info问题解决
问题: 访问www.xxxx.com/index.php/api/xxxxxxxxx网址时,提示无法访问,找不到页面 解决: 第一次,是改了nginx.conf,不会报这个错误了,但还是没有用 loc ...
- 将Excel数据导入Oracle中
第一步:修改Excel 1.将Excel的表头修改为目标数据库中表的字段名 2.去重(如果有需要的话) 删除Excel表中的重复数据: 选择去重的列: 删除成功后提示: 第二步:将修改后的Excel另 ...
- RDO部署openstack(3)
目前OpenStackNeutron框架支持的网络服务有:LoadBalancing as a Service,VPNas a Service,Firewallas a Service. 1. 安装和 ...
- Android开发——通过扫描二维码,打开或者下载Android应用
Android开发——通过扫描二维码,打开或者下载Android应用 在实现这个功能的时候,被不同的浏览器折磨的胃疼,最后实现了勉强能用,也查考了一下其他人的博客 android实现通过浏览器点击 ...
- Linux环境变量文件environment, profile, bashrc含义
转自:http://www.th7.cn/system/lin/201508/127503.shtml (1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件 ...
- opacity兼容写法
.opacity{ position: absolute; top: 0px;left: 0px; background: #000; filter:alpha(opacity=50); /* IE ...
- VB脚本调用exe应用程序并传递参数
VB脚本调用应用程序,并传递参数给exe应用程序: Private Sub CommandButton1_Click() Dim a a = Shell("D:\\ExperimentLin ...