SpringMVC项目搭建过程备忘

    一个小小的springMVC项目,看着简单,其实折腾的还是有点苦逼,仅作备忘.

    环境要求:maven3.x,spring3.x,velocity1.7,mybatis3.2.x

    项目样板,参见附件.如下为注意事项:

1) pom.xml:

    在maven3.x之后,不再支持profile.xml方式配置参数,而是改成将profile信息直接包含在pom.xml中.

2) velocity-tool:

    在velocity中使用tool时需要注意,在velocity-tool 2.0之后,xml文件的格式发生的变化,如果xml格式不正确,将导致tool无法被使用.参考:

<toolbox>
    <tool>
        <key>stringUtils</key>
        <scope>application</scope>
        <class>org.apache.commons.lang.StringUtils</class>
    </tool>
    <tool>
        <key>dateFormatUtils</key>
        <scope>application</scope>
        <class>org.apache.commons.lang.time.DateFormatUtils</class>
    </tool>
    <tool>
        <key>escapeUtils</key>
        <scope>application</scope>
        <class>org.apache.commons.lang.StringEscapeUtils</class>
    </tool>
</toolbox>

3) 在springMVC中解决请求乱码问题,在web.xml配置.

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>qipa-statistic web portal</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-velocity.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.jhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
</web-app>

4) mybatis配置文件:文件中各个标签的顺序需要一致,否则会在解析式报错.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>
	<typeAliases>
		<typeAlias type="com.test.sample.domain.TestDO" alias="TestDO" />

	</typeAliases>
	<mappers>
		<mapper resource="sqlmap/Test.xml" />
	</mappers>
</configuration>

5) mybatis中insert时返回主键的值,这个和ibatis2.x已经不一样了.

<insert id="insert" keyProperty="id">
	<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
		SELECT LAST_INSERT_ID();
	</selectKey>
	insert into test(name) value(#{name})
</insert>

 

 public Integer insert(TestDO test) {
	sqlSession.insert("Test.insert",test);
	return test.getId();
}

    项目骨架,参见附件.

你可能感兴趣的:(springMVC)