配置spring mvc ,写这篇文章的时候spring已经出了4.0 这里还是用稳定的3.2.7.RELEASE,先把spring和freemarker配置好

1.spring mvc配置

在web.xml中添加

<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml<init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-servlet.xml</param-value>  默认</init-param>--><load-on-startup>1</load-on-startup>
</servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.do</url-pattern>
</servlet-mapping>

sping通过DispatherServlet做分发,如果不指定配置文件就是项目名-servlet.xml,这里已经制定了spring-servlet.xml

这里再看spring-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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 启用spring mvc 注解 --><context:annotation-config/><!-- 设置使用注解的类所在的jar包 --><context:component-scan base-package="com.spring.controller"></context:component-scan><!-- 完成请求和注解POJO的映射 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><!-- freemarker的配置 --><bean id="freemarkerConfigurer"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/view/" /><property name="defaultEncoding" value="GBK" /><property name="freemarkerSettings"><props><prop key="template_update_delay">10</prop><prop key="locale">zh_CN</prop><prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop><prop key="date_format">yyyy-MM-dd</prop><prop key="number_format">#.##</prop></props></property></bean><!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 --><bean id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /><property name="suffix" value=".ftl" /><property name="contentType" value="text/html;charset=GBK" /><property name="exposeRequestAttributes" value="true" /><property name="exposeSessionAttributes" value="true" /><property name="exposeSpringMacroHelpers" value="true" /></bean>
</beans>

<bean id="freemarkerConfigurer"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">这个地方写入之后就报找不到类,原来光引用freemarker的类和spring-framework的类还不够,还少一个spring-context-support,添加这个类后正常了

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter  这里引用的spring版本为3.2.7 这个类已经是废弃的了,源码里注释了

** @deprecated in Spring 3.2 in favor of* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter RequestMappingHandlerAdapter}*/
@Deprecated
public class AnnotationMethodHandlerAdapter extends WebContentGeneratorimplements HandlerAdapter, Ordered, BeanFactoryAware

既然说了,那就用最新的,稍微找了下,没找到RequestMappingHandlerAdapter有什么区别,望指教

既然都配置好了,剩下就是写个controller了

package com.spring.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@Controller
@RequestMapping("/message")
public class IndexController {@RequestMapping(value = "/index", method = RequestMethod.GET)public String index(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {modelMap.put("some", "spring freemarker模板终能使用");return "index";}
}

然后是freemarker模板,非常简单,文件放到/WEB-INF/view/下

<html>
<body>
<h2>freemarker</h2><div>${some}</div>
</body>
</html>

既然配置好了,就开始启动测试下,然后就出现了各种问题。。。

首先就是几个类找不到,最经典的javax.servlet.http.HttpServletResponse和javax.servlet.http.HttpServletRequest 本来就是servlet-api.jar包里的,由于公司有私服,添加了几个都不对,先把现在正确的pom文件发下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>SpringMvc</groupId><artifactId>SpringMvc</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.apache.maven</groupId><artifactId>maven-plugin-api</artifactId>      //这个经过我的测试,发现根本不用添加<version>3.1.0</version></dependency><dependency><groupId>org.apache.tomcat</groupId><artifactId>servlet-api</artifactId>          //这个经过我的测试,发现根本不用添加<version>6.0.37</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.7.RELEASE</version></dependency><dependency><groupId>servletapi</groupId><artifactId>servlet-api</artifactId>    //这个是主要的servlet-api,在公共maven仓库上找了半天都没找对,因为我搜索的时候都是搜索的servlet-api,后来才知道直接搜servletapi就对了<version>2.4-20040521</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>3.2.6.RELEASE</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.20</version></dependency><dependency><groupId>org.springframework.ws</groupId><artifactId>spring-ws-core</artifactId>        <version>2.1.4.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><configuration><scanIntervalSeconds>1</scanIntervalSeconds><stopPort>9966</stopPort><stopKey>foo</stopKey><connectors><connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"><port>7777</port><maxIdleTime>60000</maxIdleTime></connector></connectors><webAppSourceDirectory>${basedir}/webapp</webAppSourceDirectory><webAppConfig><contextPath>/spring</contextPath></webAppConfig></configuration></plugin></plugins></build></project>

终于知道了添加servlet-api的包应该是哪个,真是郁闷http://www.mvnrepository.com/artifact/servletapi/servlet-api/2.4-20040521

启动成功了之后,访问地址http://127.0.0.1:7777/spring/message/index.do,报错

javax.servlet.ServletException: No adapter for handler [controller.UserInfoController@1470933]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

之前完全没遇上过这种错,上网查说要加了两个adapter,就可以了

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

发现不行,后来修改了spring-servlet.xml 把这里又改回来了

<!-- 完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />就可以了

废弃的方法就废弃吧,先解决问题再说

在后面就是想传对象到freemarker里,原来我记得使用过modelandview,但是好像要添加什么配置,因为使用这个访问不到放到modelandview里的对象,所以有换成了modelmap,终于完成了

这次主要目的就是在intellij上配置一个maven 的web项目,在加上spring mvc和freemarker,没想到出现这么多的问题,使用jetty插件的目的是在项目启动期间,修改freemarker模板能即时生效,而

使用独立tomcat的话,还得部署才行,而插件很方便。这些东西配置好之后,就该加上mybatis和mongodb了

转载于:https://www.cnblogs.com/t2xingzhe/p/3540769.html

intellij idea 12 搭建maven web项目 freemarker + spring mvc相关推荐

  1. idea搭建 maven web项目

    idea搭建 maven web项目 1.Create new Project 2.选择Maven,勾选Create from archetype 在archetype中选择:org.apache.m ...

  2. Java框架搭建-Maven、Mybatis、Spring MVC整合搭建

    Java框架搭建-Maven.Mybatis.Spring MVC整合搭建 1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/e ...

  3. java webpack web项目_spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

  4. IntelliJ IDEA使用(一):创建maven web项目

    在公司用eclipse开发maven web项目后,慢慢开始明白大家的那句话"受不了eclipse".的确,在开发大型的web项目,尤其是maven构建的项目,eclipse很不友 ...

  5. 荐 Intellij IDEA创建Maven Web项目(带有webapp文件夹目录的项目)

    转载自:点击打开链接 在创建项目中,IDEA提供了很多项目模板,比如Spring MVC模板,可以直接创建一个基于Maven的Spring MVC的demo,各种配置都已经设定好了,直接编译部署就可以 ...

  6. idea用maven搭建的web项目没有src目录只有pom解决方案(最暴力,最简单版本)

    用maven搭建的web项目出现的bug解决方法 解决方法: 建议先去看第二种解决方法 一.创建maven 项目 上一步完成以后就可以一直下一步了,中间可以设置一下 maven项目存储位置和名称,本次 ...

  7. IntelliJ IDEA普通的Java项目如何转成Maven Web项目

    文章目录 一.把项目改成 Java Web 项目 二.把 Java Web 项目改成 Maven 项目 三.调整目录结构 四.部署项目 五.参考 一.把项目改成 Java Web 项目 注:只有 We ...

  8. 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程

    [Spring Boot]使用Spring Boot来搭建Java web项目以及开发过程 一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来 ...

  9. Gin+Gorm+sessions 搭建 golang web项目

    Gin是用Go(Golang)编写的HTTP web框架.它具有类似Martini的API,但性能比Martini快40倍 Gorm,Golang 出色的ORM库 sessions,具有多后端支持的用 ...

最新文章

  1. xshell如何登陆堡垒机_Xshell连接有跳板机(堡垒机)的服务器
  2. SMAT,PacBio
  3. 一个学习的好去处!!
  4. 点击之后从浏览器回到微信界面的方法
  5. vue组件自定义v-model
  6. 相似邻里算法_纽约市-邻里之战
  7. 【will】JS去字符串首尾空格
  8. pytorch基础知识+构建LeNet对Cifar10进行训练+PyTorch-OpCounter统计模型大小和参数量+模型存储与调用
  9. 使用cv::findFundamentalMat要注意的几点
  10. C++ 变量在内存中的分布
  11. 内容 超链接_Word高效办公:自动创建带超链接的内容目录和图表目录
  12. 业余无线电通信_业余电台操作证书查询方式
  13. NOPIExcel读写扩展
  14. 百度NLP词 语相似度接口Demo
  15. jrebel使用方法
  16. 游戏建模师真实状况,入行4K?网上吐槽的是真的吗?
  17. android ram rom测试工具,ROM与RAM的那点事,超详细解说
  18. Node.js的线程和进程*2014年4月的文章
  19. 学习笔记-Volatility
  20. 网络隔离环境下的跨网数据传输,如何保障安全性?

热门文章

  1. python干货_Python干货整理,从入门说起(7.4)
  2. 『天涯杂谈』走的地方越多,越觉的中国的狭隘
  3. GIS之旅——研究生总结
  4. layer——极简的jquery弹出层插件
  5. PowerCMD——cmd的命令行工具
  6. 当AS3遇见Swift
  7. windows下cocos2d-x android打包
  8. WinForms和XNA
  9. JS--对象的特性之一---继承性
  10. java 动态给属性赋值_java中为实体对象的动态属性赋值