spring mvc 项目配置
2016-05-03 09:33
519 查看
本文介绍的spring mvc 项目配置要达到的目标是当访问网站静态资源的时候直接返回。
本项目视图选择原始的HTML,使用ajax访问后台控制器。
1、首先在src下面创建controller包,主要用来放控制器,看名字就知道了。
2、在网站根目录下面(web)创建static文件夹,主要是用来放静态文件(html,css,js,image等)。
3、web.xml
4、applicationContext.xml
5、dispatcher-servlet.xml
6、在controller下面添加 root.java 文件,指定一下访问网站跟目录时要打开那一个页面。当用户访问http://localhost:8080/ 或者 http://localhost:8080/index 时会打开index.html。
7、在static下创建名为index的HTML文件,在页面输入Holle Word,点击运行看效果,你就可以看到你亲爱的Holle Word了。
本项目视图选择原始的HTML,使用ajax访问后台控制器。
1、首先在src下面创建controller包,主要用来放控制器,看名字就知道了。
2、在网站根目录下面(web)创建static文件夹,主要是用来放静态文件(html,css,js,image等)。
3、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4、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" 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.xsd"> <context:component-scan base-package="controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <mvc:resources mapping="/static/**" location="/static/"/> </beans>
5、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: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.xsd"> <context:component-scan base-package="controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <mvc:resources mapping="/static/**" location="/static/"/> </beans>
6、在controller下面添加 root.java 文件,指定一下访问网站跟目录时要打开那一个页面。当用户访问http://localhost:8080/ 或者 http://localhost:8080/index 时会打开index.html。
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by zhengjiang on 2016/2/18. */ //标记为控制器,控制器URL前缀是:/。 @Controller @RequestMapping("/") public class root { //标记该action的访问路径 @RequestMapping("/") public Object start() { return "/static/index.html"; } @RequestMapping("/index") public Object index(){ return "/static/index.html";} }
7、在static下创建名为index的HTML文件,在页面输入Holle Word,点击运行看效果,你就可以看到你亲爱的Holle Word了。
相关文章推荐
- AAR文件转JAR文件的方法
- 将ISO8859-1字符串转成GB2312编码
- (转载)JAVA动态编译--字节代码的操纵
- javaweb 之sqlserver连接
- Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
- Spring--注入类型--setter
- 在JAVA中 迭代器Iterator的具体含义和用法!
- Spring中 @Autowired注解与@Resource注解的区别
- 20160502-struts2入门--ognl表达式
- 接口
- C# 和Java区别
- eclipse中使用maven插件-Dmaven.multiModuleProjectDirectory system propery is not set.
- java多线程简单demo
- 浅析Java编程中枚举类型的定义与使用
- 第60课:使用Java实战RDD与DataFrame动态转换操作
- java创建实例的底层处理
- Tabhost用法
- Spring基础-下-实战篇
- Spring基础-上-概念篇
- javaDoc和java文件的注释以及javadoc生成出现的问题【dos option选项】