配置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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://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 WebContentGenerator
implements 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了

intellij idea 12 搭建maven web项目 freemarker + spring mvc的更多相关文章

  1. intellij idea 12 搭建maven web项目 freemarker + spring mvc(续)

    上次有2个东西没整明白,一个就是controller的注解使用RequestMappingHandlerAdapter报错 No adapter for handler [controller.Use ...

  2. intellij idea 12 搭建maven web项目

    原来公司一直使用eclipse,突然使用这个intellij还真有点不习惯,等用了一段时间才发现的确好用,因为也是刚开始用,所以很多不理解的地方,搭建一个项目从头好好了解下intellij 最开始的m ...

  3. Eclipse 搭建 Maven Web项目

    第一步:安装JDK: 第二步:安装Eclipse: 第三步:安装tomcat7: 第四步:安装maven插件: 4.1 下载maven:http://maven.apache.org/download ...

  4. 在Eclipse中使用Struts和Hibernate框架搭建Maven Web项目

    前言 学习使用Java还是2012年的事情,刚开始学习的Java的时候,使用的是MyEclipse工具和SSH框架.初学者适合使用MyEclipse,因为他将struts.Spring和Hiberna ...

  5. 搭建maven web项目并配置quartz定时任务【业务:对比数据变化内容】 历程

    搭建maven web项目并配置quartz定时任务[业务:对比数据变化内容] 历程2018年03月03日 10:51:10 守望dfdfdf 阅读数:100更多个人分类: 工作 问题编辑版权声明:本 ...

  6. 搭建 maven 项目 搭建 maven web 项目及遇到 JDK 的问题

    临时起意搭建一个 maven web 项目.使用的servlet 3.0 及 1.8 JDK. maven 默认创建了一个JDK 1.5 版本的项目. 注意此处选择一下WAR包.不然在配置中配置的话会 ...

  7. Eclipse搭建maven web项目

    最近在做做一个小实验,搭建ssm框架,要求使用maven来统一管理jar包,接下来就看如何建立maven项目,首先必须有要有相应的开发环境:JDK和maven,以及配置tomcat. 开发环境搭建可以 ...

  8. eclipse搭建maven project的spring4 spring mvc mybatis

    一,先确定已经安装好了Eclipse Java EE IDE for Web Developers我用的是如下版本 Version: Neon.3 Release (4.6.3)Build id: 2 ...

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

    1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr 选择 ...

随机推荐

  1. Tomcat启动时卡在“INFO: Deploying web application directory ”

    今天在linux上的tomcat部署一个网站时,在刚启动tomcat的时候提示启动成功,然后也能访问成功. 可是第二次启动时虽然没有报错,但无法访问tomcat,查看了catalina.out日志,发 ...

  2. php 写商城网站的总结吧

    ---恢复内容开始--- 在兄弟连培训,这半个月在做一期项目,期间学到了很多东西,可是还有好多没有学会灵活运用.今天在登录界面加入验证码的时候,form提交不过去input里面的验证码,session ...

  3. 用C++实现的八皇后问题

    我是一个C++初学者,控制台实现了一个八皇后问题. 代码如下: //"八皇后问题"V1.0 //李国良于2017年1月11日编写完成 #include <iostream&g ...

  4. postgresql----serial类型和序列

    postgresql序列号(SERIAL)类型包括smallserial(smallint,short),serial(int)和bigserial(bigint,long long int),不管是 ...

  5. 如何用php开启企业微信开发的回调模式

    猜想: 懵逼 实践: 微信公众号开发的手册中甚至给出了只需要修改几个参数就能使用的范例.企业微信开发中在一个很不显眼的地方放了一个sample. https://work.weixin.qq.com/ ...

  6. CSS3 background-size:cover/contain

    background-size的cover和contain指定背景图片的自适应方式,只能对整张图片进行缩放. cover是拉伸图片使之充满元素,元素肯定是被铺满的,但是图片有可能显示不全. conta ...

  7. java 包 修饰符 权限详解

    作用域   当前类    同package   子孙类 其他package  public √   √  √ √  protected √ √ √ ×  friendly(default) √ √ × ...

  8. LoadLibraryW 参数问题

    error C2664: "LoadLibraryW": 不能将参数1 从"const char [8]"转换为"LPCWSTR" 右击工程 ...

  9. vim编辑器设置文件的fileformat

    问题:dos格式文件传输到centos系统时,会在每行的结尾多一个^M,即dos文件中的换行符"\r\n"会被转换为unix文件中的换行符"\n",而此文件若是 ...

  10. centos 6.5 安装openssl

    1.下载wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz 2.解压tar zxf openssl-1.0.2h.tar.gzcd op ...