springmvc setup: web.xml, webapp-servlet.xml, and webapp-servlet-main.xml, pom.xml, logback.xml


/ Published in: Java
Save to your folder(s)

There are multiple file snippets here. Careful when cut/paste

mvn archetype:create -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=deng -DartifactId=myspringmvc
touch WEB-INF/conf.properties


Copy this code and paste it in your HTML
  1. WEB-INF/web.xml:
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://java.sun.com/xml/ns/javaee"
  5. xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  6. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  7. version="2.4">
  8. <description></description>
  9. <display-name>My SpringMVC Webapp</display-name>
  10.  
  11. <!-- Spring config -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>/WEB-INF/applicationContext.xml</param-value>
  15. </context-param>
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19. <servlet>
  20. <servlet-name>webapp</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>webapp</servlet-name>
  26. <url-pattern>/webapp/*</url-pattern>
  27. </servlet-mapping>
  28. </web-app>
  29.  
  30. WEB-INF/webapp-server.xml
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33. xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang"
  34. xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
  35. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
  36. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  37.  
  38. <!-- Properties Configurer with "/" refer to root of webappRealPath -->
  39. <bean id="propertyConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
  40. <property name="ignoreResourceNotFound" value="true"></property>
  41. <property name="locations">
  42. <util:list>
  43. <value>/WEB-INF/conf.properties</value>
  44. </util:list>
  45. </property>
  46. </bean>
  47.  
  48. <!-- Message resouces -->
  49. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  50. <property name="basename" value="messages" />
  51. </bean>
  52.  
  53. <!-- SpringMVC Beans -->
  54. <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  55. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  56. <property name="prefix" value="/WEB-INF/view/" />
  57. <property name="suffix" value=".jsp" />
  58. </bean>
  59.  
  60. <!-- Imports -->
  61. <import resource="webapp-servlet-main.xml" />
  62. </beans>
  63.  
  64.  
  65. WEB-INF/webapp-server-main.xml
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68. xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang"
  69. xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
  70. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
  71. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  72.  
  73. <!-- Url to controller mappings -->
  74. <bean id="mainControllerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  75. <property name="urlMap">
  76. <util:map>
  77. <entry key="main" value-ref="mainController" />
  78. </util:map>
  79. </property>
  80. </bean>
  81.  
  82. <bean id="mainController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>
  83. </beans>
  84.  
  85. pom.xml:
  86. <project xmlns="http://maven.apache.org/POM/4.0.0"
  87. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  89. <modelVersion>4.0.0</modelVersion>
  90. <groupId>deng</groupId>
  91. <artifactId>myspringmvc</artifactId>
  92. <packaging>war</packaging>
  93. <version>1.0-SNAPSHOT</version>
  94. <name>myspringmvc Maven Webapp</name>
  95. <url>http://maven.apache.org</url>
  96.  
  97. <profiles>
  98. <profile>
  99. <id>servlet</id>
  100. <activation>
  101. <activeByDefault>false</activeByDefault>
  102. </activation>
  103. <dependencies>
  104. <dependency>
  105. <groupId>javax.servlet</groupId>
  106. <artifactId>servlet-api</artifactId>
  107. <version>2.5</version>
  108. <scope>provided</scope>
  109. </dependency>
  110. <dependency>
  111. <groupId>javax.servlet.jsp</groupId>
  112. <artifactId>jsp-api</artifactId>
  113. <version>2.1</version>
  114. <scope>provided</scope>
  115. </dependency>
  116. </dependencies>
  117. </profile>
  118. </profiles>
  119. <dependencies>
  120. <dependency>
  121. <groupId>junit</groupId>
  122. <artifactId>junit</artifactId>
  123. <version>4.4</version>
  124. <scope>test</scope>
  125. </dependency>
  126. <dependency>
  127. <groupId>javax.servlet</groupId>
  128. <artifactId>jstl</artifactId>
  129. <version>1.1.2</version>
  130. </dependency>
  131. <dependency>
  132. <groupId>taglibs</groupId>
  133. <artifactId>standard</artifactId>
  134. <version>1.1.2</version>
  135. </dependency>
  136.  
  137. <dependency>
  138. <groupId>org.springframework</groupId>
  139. <artifactId>spring</artifactId>
  140. <version>2.0.6</version>
  141. </dependency>
  142. <dependency>
  143. <groupId>org.slf4j</groupId>
  144. <artifactId>slf4j-api</artifactId>
  145. <version>1.4.3</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>ch.qos.logback</groupId>
  149. <artifactId>logback-classic</artifactId>
  150. <version>0.9.8</version>
  151. </dependency>
  152. <dependency>
  153. <groupId>org.slf4j</groupId>
  154. <artifactId>jcl104-over-slf4j</artifactId>
  155. <version>1.4.3</version>
  156. </dependency>
  157. <dependency>
  158. <groupId>commons-lang</groupId>
  159. <artifactId>commons-lang</artifactId>
  160. <version>2.3</version>
  161. </dependency>
  162. </dependencies>
  163. <build>
  164. <finalName>myspringmvc</finalName>
  165. <plugins>
  166. <plugin>
  167. <groupId>org.mortbay.jetty</groupId>
  168. <artifactId>maven-jetty-plugin</artifactId>
  169. </plugin>
  170. <plugin>
  171. <artifactId>maven-compiler-plugin</artifactId>
  172. <configuration>
  173. <source>1.5</source>
  174. <target>1.5</target>
  175. </configuration>
  176. </plugin>
  177. </plugins>
  178. </build>
  179. </project>
  180.  
  181. src/test/resources/logback-test.xml:
  182. <?xml version="1.0" encoding="UTF-8" ?>
  183. <configuration>
  184. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  185. <layout class="ch.qos.logback.classic.PatternLayout">
  186. <pattern>%-5level [%20.20logger:%-4line] - %msg%n</pattern>
  187. </layout>
  188. </appender>
  189.  
  190. <logger name="deng.myspringmvc">
  191. <level value="debug" />
  192. </logger>
  193. <logger name="org.springframework">
  194. <level value="info" />
  195. </logger>
  196.  
  197. <root>
  198. <level value="error" />
  199. <appender-ref ref="STDOUT" />
  200. </root>
  201. </configuration>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.