配置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了

posted on 2014-02-10 15:55  程序猿_哲  阅读(4356)  评论(0编辑  收藏  举报