首页 > 学院 > 开发设计 > 正文

Java实现视频解析系列001:Spring MVC入门:Hello World

2019-11-14 22:35:50
字体:
来源:转载
供稿:网友
java实现视频解析系列001:SPRing MVC入门:Hello Worldspring下载

http://projects.spring.io/spring-framework/

安装spring

有两种方式建立项目,建议使用maven项目方式,可以更好的控制jar包依赖。

dynamic web project方式

在maven中作如下配置即可(关于maven请参考本站关于maven的文章)。 关于dynamic web project转换为maven请参考:eclipse java工程跟maven工程的互相转换 加入spring mvc的方式,直接在pom.xml里面加入以下代码即可:

<!--spring -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.2.13.RELEASE</version>

<type>jar</type>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>3.2.13.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>3.2.13.RELEASE</version>

<type>jar</type>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>3.2.13.RELEASE</version>

</dependency>

注意加入后要更新eclipse项目。 QQ截图20150113145555

maven项目方式项目结构

2015-01-15_094937

配置与Hello World基于maven

1、修改pom.xml,添加必要依赖

2、添加配置文件

applicationContext.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:context="http://www.springframework.org/schema/context"

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">

</beans>

SpringMVC-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:context="http://www.springframework.org/schema/context"

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

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.0.xsd">

<!-- 指定自动扫描路径 -->

<context:component-scan base-package="com.terwer.player.action"></context:component-scan>

<!-- 视图解析器,使用jstl -->

<bean id="viewResolver"

class="org.springframework.web.servlet.view.UrlBasedViewResolver">

<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

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

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

</bean>

</beans>

web.xml

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<display-name>Archetype Created Web Application</display-name>

<!--spring 监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--spring MVC拦截器 -->

<servlet>

<servlet-name>springMvc</servlet-name>

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

<init-param>

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

<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>

</init-param>

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

</servlet>

<servlet-mapping>

<servlet-name>springMvc</servlet-name>

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

</servlet-mapping>

</web-app>

3、在WEB-INF/views添加页面index.jsp

QQ截图20150115100250

传统方式

参考maven方式

运行在浏览器输入

http://localhost:8080/player/home/index

效果

QQ截图20150115100702

全部源码下载

Github download

系列导航

http://www.terwer.com/special.html#用Java实现视频地址解析


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表