1、首先是创建maven项目,我把core和webapp分开了
再按上面的方法创建 webapp工程
注:add dependency的注意group id和 artifact id和你要依赖的项目是一致的,在core里的pom.xml里可以看到
2、web.xml的配置如下,主要的两个配置,一个listener一个DispatcherServlet
<?xml version="1.0"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>alijk</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>alijkServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-restful.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>alijkServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、applictionContext配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
</beans>
4、applictionContext-restful.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- Scan for annotation based controllers -->
//加入扫描
<context:component-scan base-package="alijk.dataView.services" />
//返回json时,不配置不报错
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- StringHttpMessageConverter编码为UTF-8,防止乱码 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
//静态资源的配置,不配置的话,找不到js和css
<mvc:resources mapping="/styles/**" location="/WEB-INF/styles/" />
<mvc:resources mapping="/script/**" location="/WEB-INF/script/" />
<mvc:default-servlet-handler />
//配置mvc跳转页面时,去哪查换jsp
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
5、webapp的目录结构如下
6、core的pom.xml配置,里面引入spring的jar包,以及配置maven发布项目时发布的信息
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>alijk</groupId>
<artifactId>alijk-dataView-core</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>alijk-dataView-core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Application settings -->
<copyright.year>2009</copyright.year>
<dao.framework>ibatis</dao.framework>
<web.framework>spring</web.framework>
<amp.genericCore>true</amp.genericCore>
<amp.fullSource>true</amp.fullSource>
<jmock.version>2.4.0</jmock.version>
<jsp.version>2.0</jsp.version>
<junit.version>4.6</junit.version>
<servlet.version>2.4</servlet.version>
<wiser.version>1.2</wiser.version>
<!-- WebTest dependency versions -->
<webtest.version>R_1702</webtest.version>
<jdbc.groupId>abysslib</jdbc.groupId>
<jdbc.artifactId>ojdbc14</jdbc.artifactId>
<jdbc.version>10.2.0.4</jdbc.version>
<!-- Properties calculated by AppFuse when running full-source plugin -->
<aspectj.version>1.6.8</aspectj.version>
<clickstream.version>1.0.2</clickstream.version>
<commons.beanutils.version>1.7.0</commons.beanutils.version>
<commons.collections.version>3.2.1</commons.collections.version>
<commons.dbcp.version>1.4</commons.dbcp.version>
<commons.pool.version>1.4</commons.pool.version>
<commons.fileupload.version>1.2.1</commons.fileupload.version>
<commons.io.version>1.4</commons.io.version>
<commons.validator.version>1.3.1</commons.validator.version>
<commons.math.version>1.2</commons.math.version>
<displaytag.version>1.1.1</displaytag.version>
<dwr.version>3.0.M1</dwr.version>
<ehcache.version>2.3.1</ehcache.version>
<hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
<hibernate.version>3.2.6.ga</hibernate.version>
<javamail.version>1.4</javamail.version>
<jpa.version>1.0</jpa.version>
<jstl.version>1.1.2</jstl.version>
<log4j.version>1.2.15</log4j.version>
<logback.version>1.1.1</logback.version>
<oscache.version>2.4</oscache.version>
<sitemesh.version>2.4.2</sitemesh.version>
<springmodules.validation.version>0.8</springmodules.validation.version>
<struts.menu.version>2.4.3</struts.menu.version>
<urlrewrite.version>3.0.4</urlrewrite.version>
<velocity.version>1.7</velocity.version>
<click.version>2.3.0-RC1</click.version>
<ibatis2.version>2.3.4.726</ibatis2.version>
<ibatis3.version>3.0</ibatis3.version>
<trail.version>1.1</trail.version>
<cglib.version>2.2</cglib.version>
<slf4j.version>1.7.6</slf4j.version>
<ojdbc.version>10.2.0.4</ojdbc.version>
<abyss.version>1.4.0</abyss.version>
<loquat.version>0.3.6</loquat.version>
<spring.version>3.2.8.RELEASE</spring.version>
<spring.security.version>3.2.3.RELEASE</spring.security.version>
<commons.digester.version>1.7</commons.digester.version>
<commons.validator.version>1.3.1</commons.validator.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons.dbcp.version}</version>
</dependency>
<!-- Spring Framework Dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>quartz</artifactId>
<groupId>quartz</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security Dependencies -->
<!--
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
</dependencies>
</project>
7、以后配置已经完成,下面看index。jsp
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
//直接跳转到一个tolist.do
<jsp:forward page="/dataView/tolist.do"></jsp:forward>
</body>
</html>
8、service代码
@Controller
@Scope(value = "prototype")
@RequestMapping("/dataView")
public class GarudaDataViewController {
@RequestMapping("/tolist.do")
public String login() {
System.out.println("----------come----------");
return "dataView";
}
9、这样就会跳转到page/dataView.jsp了,dataView.jsp
<div class="wapper">
<div class="searcher">
<div><button style="height:30px;" onclick="runSql();">查询</button>
<span>注:1、每页显示20条数据,大于20条时会有分页按钮。</span>
<span> 2、sql错误会显示在页面中。<span>
</div><br/>
<textarea rows="10" style="width: 90%;resize: none;" id="sqlText"></textarea>
</div>
<br>
10、启动项目后出现的页面
注:在配置的过程中,出现很多次错误 ,现把错误总结
1、无法访问js,引入不了jquery,min.js,问题:没配置静态资源的访问,在xml中加入以下代码
<mvc:resources mapping="/styles/**" location="/WEB-INF/styles/" />
<mvc:resources mapping="/script/**" location="/WEB-INF/script/" />
<mvc:default-servlet-handler />
2、可以访问页面后,请求后台时返回数据出错 ,406 错误,问题:缺少配置,和jar包,加入配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- StringHttpMessageConverter编码为UTF-8,防止乱码 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
并且加入jar包:jackson-core-asl.jar jackson-mapper.asl.jar
最终项目还是成功的运行起来了,文笔不好,只是为了记录下整个过程,看者务喷