Spring搭建MVC WEB项目[转]
2014-12-16 14:06
423 查看
原文链接:/article/1354250.html
1.创建一个web项目
2.假设,我们已经安装完毕Spring所需要的依赖包,以及一些其它的扩展包,以及Jetty容器,ps:Jetty容器安装看上一节文章。
3.运行web项目,必须有web.xml配置文件,web.xml放置在WebContent/WEB-INF/目录下面。
[html]viewplaincopyprint?
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<!--配置文件位置,默认为/WEB-INF/applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--上下文Spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--servlet控制跳转,这边默认回去走spring-servlet.xml的xml文件-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
</servlet>
<!--url-pattern是Spring监听路由过来的方式,然后去寻找匹配的Controller
例如:
<url-pattern>/</url-pattern>一般是/hello/say/这样的URL方式
<url-pattern>*.htm</url-pattern>一般是/hello/say.htm这样的URL方式
-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.运行Spring需要有applicationContext.xml这个配置文件,我们也将applicationContext.xml放置在WEB-INF/目录下。
[html]viewplaincopyprint?
<?xmlversion="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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<context:annotation-config/>
<aop:aspectj-autoproxy/>
</beans>
5.spring-servlet.xml配置了具体的Spring需要访问的Controller文件夹目录以及模板的目录和模板的后缀名称。
[html]viewplaincopyprint?
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--访问com.mvc.rest包下有@Controller注解的Controller文件-->
<context:component-scanbase-package="com.mvc.rest"/>
<!--通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter-->
<mvc:annotation-driven/>
<!--视图配置,配置试图的目录,后缀名等-->
<beanid="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/views/"/>
<propertyname="suffix"value=".jsp"></property>
</bean>
</beans>
6.我们需要创建一个com.mvc.rest包,在包下面创建一个名为:HelloController.java的文件。这个文件就是MVC的控制器。那么,我们有两个方法,分别为say和yes。url中访问分别是:/hello/say/和/hello/yes/
[java]viewplaincopyprint?
packagecom.mvc.rest;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
//@Controller是一个标识这个是控制器类的注解标签,如果是控制器类都需要有这个注解。
@Controller
//@RequestMapping(value="/hello")会映射到url/hello/则访问HelloController中的Action
@RequestMapping(value="/hello")
publicclassHelloController{
//@RequestMapping(value="/say")会映射到url/hello/say则访问HelloController中的Action
@RequestMapping(value="/say")
publicvoidsay(){
System.out.print("thisisHelloControllerAndsayAction\r\n");
}
@RequestMapping(value="/yes")
publicvoidyes(){}
}
7.我们需要在/WEB-INF/目录下创建一个views的目录,然后再创建一个/views/hello/的目录,里面分别是say.jsp和yes.jsp,是模板文件。
8.然后运行配置,通过Jetty容器,运行web程序。
9.运行完毕后,控制台会出现以下信息:
10.最后在URL中分别访问:http://127.0.0.1:8090/hello/say/和http://127.0.0.1:8090/hello/yes/成功!
11.详细SpringController部分的注解,请阅读:http://zachary-guo.iteye.com/blog/1318597
1.创建一个web项目
2.假设,我们已经安装完毕Spring所需要的依赖包,以及一些其它的扩展包,以及Jetty容器,ps:Jetty容器安装看上一节文章。
3.运行web项目,必须有web.xml配置文件,web.xml放置在WebContent/WEB-INF/目录下面。
[html]
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
id="WebApp_ID"version="2.5">
<!--配置文件位置,默认为/WEB-INF/applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--上下文Spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--servlet控制跳转,这边默认回去走spring-servlet.xml的xml文件-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
</servlet>
<!--url-pattern是Spring监听路由过来的方式,然后去寻找匹配的Controller
例如:
<url-pattern>/</url-pattern>一般是/hello/say/这样的URL方式
<url-pattern>*.htm</url-pattern>一般是/hello/say.htm这样的URL方式
-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.运行Spring需要有applicationContext.xml这个配置文件,我们也将applicationContext.xml放置在WEB-INF/目录下。
[html]
<?xmlversion="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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
">
<context:annotation-config/>
<aop:aspectj-autoproxy/>
</beans>
5.spring-servlet.xml配置了具体的Spring需要访问的Controller文件夹目录以及模板的目录和模板的后缀名称。
[html]
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<!--访问com.mvc.rest包下有@Controller注解的Controller文件-->
<context:component-scanbase-package="com.mvc.rest"/>
<!--通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter-->
<mvc:annotation-driven/>
<!--视图配置,配置试图的目录,后缀名等-->
<beanid="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/views/"/>
<propertyname="suffix"value=".jsp"></property>
</bean>
</beans>
6.我们需要创建一个com.mvc.rest包,在包下面创建一个名为:HelloController.java的文件。这个文件就是MVC的控制器。那么,我们有两个方法,分别为say和yes。url中访问分别是:/hello/say/和/hello/yes/
[java]
packagecom.mvc.rest;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
//@Controller是一个标识这个是控制器类的注解标签,如果是控制器类都需要有这个注解。
@Controller
//@RequestMapping(value="/hello")会映射到url/hello/则访问HelloController中的Action
@RequestMapping(value="/hello")
publicclassHelloController{
//@RequestMapping(value="/say")会映射到url/hello/say则访问HelloController中的Action
@RequestMapping(value="/say")
publicvoidsay(){
System.out.print("thisisHelloControllerAndsayAction\r\n");
}
@RequestMapping(value="/yes")
publicvoidyes(){}
}
7.我们需要在/WEB-INF/目录下创建一个views的目录,然后再创建一个/views/hello/的目录,里面分别是say.jsp和yes.jsp,是模板文件。
8.然后运行配置,通过Jetty容器,运行web程序。
9.运行完毕后,控制台会出现以下信息:
10.最后在URL中分别访问:
11.详细SpringController部分的注解,请阅读:http://zachary-guo.iteye.com/blog/1318597
相关文章推荐
- IDEA上面搭建一个SpringBoot的web-mvc项目
- IDEA上面搭建一个SpringBoot的web-mvc项目
- SpringMvc+MyBatis+MySQL+Maven+Redis搭建一个基础的web项目
- IDEA上面搭建一个SpringBoot的web-mvc项目遇到的问题
- 搭建基于springmvc+freemarker+maven的web项目
- springmvc+maven搭建web项目
- intellij idea 12 搭建maven web项目 freemarker + spring mvc
- 【web开发】Eclipse中SpringMVC+Mybatis+Maven搭建的项目框架(SMM框架)
- 基于Maven项目,搭建Spring+SpringMVC+MyBatis的WEB项目(待更新)
- 关于maven+springMVC+mybatis搭建的web项目问题
- Java 学习笔记09:Spring搭建MVC WEB项目
- 手把手带你搭建 SpringMVC + Zookeeper +Dubbo(一)【Maven 创建 Web项目】
- 从零开始搭建maven+springmvc+mybatis web项目(3)---springmvc流程测试
- spring SpringMVC MyBatis 搭建项目 MavenWeb项目
- 从零开始搭建maven+springmvc+mybatis web项目(1)---maven项目目录创建
- intellij idea 12 搭建maven web项目 freemarker + spring mvc(续)
- 使用myeclipse搭建带maven的ava web项目(Spring+SpringMVC+Mybatis)
- 搭建基于SpringMVC+MyBatis+Maven的web项目
- Spring Web MVC项目搭建(一)
- IDEA上面搭建一个SpringBoot的web-mvc项目