intellij idea 12 搭建maven web項目 freemarker + spring mvc


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM