java常用框架SpringMVC3/4入门教程

作者 xqlee
浏览次数 1430


java常用框架SpringMVC3/4入门教程

1SpringMVC3

1.1项目结构图

java常用框架SpringMVC3/4入门教程

1.2Pom.xml

<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>seven.org</groupId>
   <artifactId>springmvc3</artifactId>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>springmvc3 Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <properties>
      <junit.version>4.12</junit.version>
      <spring.version>3.2.2.RELEASE</spring.version>
      <org.codehaus.jackson.version>1.9.13</org.codehaus.jackson.version>
      <commons-fileupload.version>1.3.1</commons-fileupload.version>
   </properties>
   <dependencies>
      <dependency>
         <!-- test -->
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
      </dependency>
 
      <!-- spring framework start -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</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-web</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- spring framework end -->
 
      <!-- 为SpringMVC提供json数据类型支持 -->
 
      <!-- 注意,新版的 jackson-core-asl已经升级为Group:com.fasterxml.jackson.core|Artifact:jackson-core在SpringMVC4中需要使用新版 -->
      <dependency>
         <groupId>org.codehaus.jackson</groupId>
         <artifactId>jackson-core-asl</artifactId>
         <version>${org.codehaus.jackson.version}</version>
      </dependency>
      <dependency>
         <groupId>org.codehaus.jackson</groupId>
         <artifactId>jackson-mapper-asl</artifactId>
         <version>${org.codehaus.jackson.version}</version>
      </dependency>
 
      <!-- el 标签库 -->
      <!-- standard.jar -->
      <dependency>
         <groupId>taglibs</groupId>
         <artifactId>standard</artifactId>
         <version>1.1.2</version>
      </dependency>
      <!-- JSTL -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.1.2</version>
      </dependency>
      <!-- /el 标签库 -->
 
      <!-- 文件上传下载 -->
      <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>${commons-fileupload.version}</version>
      </dependency>
 
      <!-- slf4j 和 log4j合用的Maven配置 -->
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>1.7.2</version>
      </dependency>
 
   </dependencies>
   <build>
      <finalName>springmvc3</finalName>
   </build>
</project>
 

1.3web.xml

<web-app 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_3_0.xsd"
   version="3.0">
   <display-name>Archetype Created Web Application</display-name>
 
   <!-- 字符集处理 -->
   <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>
   </filter>
   <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
 
 
   <!-- spring 配置 -->
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/cfg/spring/spring-beans.xml</param-value>
   </context-param>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 
   <!-- 配置缓存清除监听器,负责处理由 JavaBean Introspector 功能而引起的缓存泄露 -->
   <listener>
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
   </listener>
 
 
   <!-- springMVC配置 -->
   <servlet>
      <servlet-name>springMVCSerlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:/cfg/spring/spring-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>springMVCSerlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
 
   <!-- 添加下面容器,防止同一个服务器部署多个项目web.root冲突 -->
   <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>springMVC3</param-value>
   </context-param>
 
 
   <!-- log4j配置 -->
   <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:/cfg/log/Log4j.properties</param-value>
   </context-param>
   <context-param>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>60000</param-value>
   </context-param>
 
   <!-- session timeout -->
   <session-config>
      <!-- 分钟 -->
      <session-timeout>60</session-timeout>
   </session-config>
 
   <!-- welcome page -->
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
 
   <!-- session copy -->
   <distributable />
</web-app>
 

1.4spring-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:mvc="http://www.springframework.org/schema/mvc"
   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.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
 
   <!-- 自动扫描包 -->
   <context:component-scan base-package="com" />
 
   <!-- 注解驱动 -->
   <mvc:annotation-driven />
   <mvc:default-servlet-handler />
   <!-- 资源管理不拦截 -->
   <mvc:resources location="/resources/" mapping="/resources/**" />
   <mvc:resources location="/upload/" mapping="/upload/**" />
 
   <!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp -->
   <bean id="internalResourceViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 前缀 -->
      <property name="prefix" value="/WEB-INF/pages/" />
      <!-- 后缀 -->
      <property name="suffix" value="" />
   </bean>
 
 
   <!-- 上传图片/文件需要配置 -->
   <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="104857600" />
      <property name="maxInMemorySize" value="4096" />
   </bean>
 
 
   <!-- 配置JSON支持 start -->
   <bean id="mappingJacksonHttpMessageConverter"
   class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
      <property name="supportedMediaTypes">
         <list>
            <!-- 处理返回的json数据的编码,默认是ISO-88859-1 -->
            <value>text/html;charset=UTF-8</value>
         </list>
      </property>
   </bean>
   <bean
   class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <!-- messageConverters属性中加入 mappingJacksonHttpMessageConverter用来处理json数据类行 -->
      <property name="messageConverters">
         <list>
            <ref bean="mappingJacksonHttpMessageConverter" />
         </list>
      </property>
   </bean>
   <!-- 配置JSON支持 end -->
 
 
</beans>

1.5spring-beans.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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
              http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-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/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">
   <context:annotation-config />
   <!-- 启动自动扫描该包下所有的注解(如@controller) -->
   <context:component-scan base-package="com" />
 
</beans>
 
 

1.6Log4j配置文件

#defind the rooeLogger ,the rootlogger min level is INFO,and two way to output log
log4j.rootLogger = INFO,FILE,CONSOLE,DATABASE
 
#----------------------------------------------------------------------
#defind the FILE as file everyday                                     |
#----------------------------------------------------------------------
log4j.appender.FILE = org.apache.log4j.DailyRollingFileAppender
 
#the log min level set as INFO
log4j.appender.FILE.Threshold=INFO
 
#log file encoding set UTF-8
log4j.appender.FILE.encoding=UTF-8
 
#log file path
log4j.appender.FILE.File=/home/tomcat/log/springmvc3/springmvc3
 
#log file name append
log4j.appender.FILE.DatePattern='_'yyyy-MM-dd'.log'
 
#Immediate write the log is true
log4j.appender.FILE.ImmediateFlush=true
 
#the layout of the log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
 
#the detail layout of log  in the log file
log4j.appender.FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} Springmvc3 %-5p [%c:%L] | %m%n
 
#-------------------------------------------------------------------------
#this use the 'org.apache.log4j.ConsoleAppender' to output in the Console|
#-------------------------------------------------------------------------
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 
#output min level is INFO
log4j.appender.Threshold=INFO
 
#the output target is Console
log4j.appender.CONSOLE.Target=System.out
 
#the layout of the log
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} springmvc3 %-5p | %m%n
 
#----------------------------------------------------------------------------
#insert the log into database                                               |
#----------------------------------------------------------------------------
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
#The jdbc url
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/springmvc3
#The jdbc driver
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
#DB user
log4j.appender.DATABASE.user=root
#DB password
log4j.appender.DATABASE.password=root
#sql
log4j.appender.DATABASE.sql=INSERT INTO zhl_log (operationTime,logLevel,logClass,logDetail)  VALUES ("%d{yyyy-MM-dd HH:mm:ss}", "%-5p","%F:%L", "%m")
#layout
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#layout detail
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

1.7测试vo
package com.xqlee.springmvc.vo;
 
/**
 *
 *
 * <pre>
 * @功能说明 [测试bean]
 * @其他说明 [暂无]
 * @文件编码 [UTF-8]
 * @所属包名 [com.xqlee.springmvc.vo]
 * @项目名称 [cdws]
 * @创建日期 []
 * @创建人员 [lxq]
 * @最后修改 []
 * @修改说明 [暂无更新]
 * @修改人员 [lxq]
 * @公司组织 [org]
 * @版本说明 [v 1.0]
 *
 * </pre>
 */
public class Tobject {
 
   private String id;
   private String name;
   private String num;
 
   public Tobject(String id, String name, String num) {
      this.id = id;
      this.name = name;
      this.num = num;
   }
 
   public String getId() {
      return id;
   }
 
   public void setId(String id) {
      this.id = id;
   }
 
   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }
 
   public String getNum() {
      return num;
   }
 
   public void setNum(String num) {
      this.num = num;
   }
 
}

1.8测试controller
package com.xqlee.springmvc.controller;
 
import java.util.Vector;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.xqlee.springmvc.vo.Tobject;
 
/**
 *
 *
 * <pre>
 * @功能说明 [测试类]
 * @其他说明 [暂无]
 * @文件编码 [UTF-8]
 * @所属包名 [com.xqlee.springmvc.controller]
 * @项目名称 [cdws]
 * @创建日期 []
 * @创建人员 [lxq]
 * @最后修改 []
 * @修改说明 [暂无更新]
 * @修改人员 [lxq]
 * @公司组织 [org]
 * @版本说明 [v 1.0]
 *
 * </pre>
 */
@Controller(value = "/beanTestController.do")
public class BeanTestController {
   @RequestMapping(params = "method=Vector")
   // @ResponseBody
   public Object testVector(HttpServletRequest request,
         HttpServletResponse response) {
      // 创建容器
      Vector<Tobject> vtb = new Vector<Tobject>();
      Tobject tb;
      for (int i = 0; i <= 5; i++) {
         tb = new Tobject("ID-" + i, "名称Name-" + i, "Num-" + i);
         vtb.add(tb);
      }
      request.setAttribute("vtb", vtb);
      return "../../vector.jsp";
   }
 
   @RequestMapping(params = "method=testJson")
   @ResponseBody
   public Object testJson(HttpServletRequest request,
         HttpServletResponse response) {
      // 创建容器
      Vector<Tobject> vtb = new Vector<Tobject>();
      Tobject tb;
      for (int i = 0; i <= 5; i++) {
         tb = new Tobject("ID-" + i, "名称Name-" + i, "Num-" + i);
         vtb.add(tb);
      }
      return vtb;
   }
 
}
 

1.9index.jsp

<!DOCTYPE>
<html>
<body>
   <h2>Hello World!</h2>
   <a href="beanTestController.do?method=Vector">Vector Test</a>
</body>
</html>

1.10vector.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE>
<html>
<body>
   <table border='1'>
      <thead>
         <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Num</td>
         </tr>
      </thead>
      <tbody>
         <c:forEach items="${vtb}" var="Object">
            <tr>
                <td>${Object.id}</td>
                <td>${Object.name}</td>
                <td>${Object.num}</td>
            </tr>
         </c:forEach>
      </tbody>
   </table>
 
 
</body>
</html>

2 SpringMVC4

2.1项目结构图

2

2.2pom.xml

<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>seven.org</groupId>
   <artifactId>springMVC</artifactId>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>springMVC Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <!-- 属性版本 -->
   <properties>
      <junit.version>4.12</junit.version>
      <spring.version>4.1.7.RELEASE</spring.version>
      <org.slf4j.version>1.7.2</org.slf4j.version>
      <mysql.driver.version>5.1.32</mysql.driver.version>
   <com.fasterxml.jackson.jaxrs.version>2.4.1</com.fasterxml.jackson.jaxrs.version>
      <commons-fileupload.version>1.3.1</commons-fileupload.version>
 
   </properties>
   <dependencies>
 
      <!-- 测试所需 -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
      </dependency>
 
      <!-- spring core -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</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-web</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
      </dependency>
 
      <!-- MySQL数据库驱动 导入 start -->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql.driver.version}</version>
      </dependency>
      <!-- MySQL数据库驱动 导入 end -->
 
 
      <!-- el 标签库 -->
      <!-- standard.jar -->
      <dependency>
         <groupId>taglibs</groupId>
         <artifactId>standard</artifactId>
         <version>1.1.2</version>
      </dependency>
      <!-- JSTL -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.1.2</version>
      </dependency>
      <!-- /el 标签库 -->
 
      <!-- 处理json -->
      <dependency>
         <groupId>com.fasterxml.jackson.jaxrs</groupId>
         <artifactId>jackson-jaxrs-xml-provider</artifactId>
         <version>${com.fasterxml.jackson.jaxrs.version}</version>
      </dependency>
 
      <!-- 文件上传下载 -->
      <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>${commons-fileupload.version}</version>
      </dependency>
 
      <!-- slf4j 和 log4j合用的Maven配置 -->
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>${org.slf4j.version}</version>
      </dependency>
 
   </dependencies>
   <build>
      <finalName>springMVC</finalName>
   </build>
</project>

2.3web.xml
<web-app 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_3_0.xsd"
   version="3.0">
   <display-name>Archetype Created Web Application</display-name>
 
   <!-- 字符集处理 -->
   <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>
   </filter>
   <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
 
 
   <!-- spring 核心配置 -->
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/cfg/spring/spring-beans.xml</param-value>
   </context-param>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 
   <!-- 配置缓存清除监听器,负责处理由 JavaBean Introspector 功能而引起的缓存泄露 -->
   <listener>
      <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
   </listener>
 
 
   <!-- springMVC配置 -->
   <servlet>
      <servlet-name>springMVCSerlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:/cfg/spring/spring-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>springMVCSerlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
 
   <!-- 添加下面容器,防止同一个服务器部署多个项目web.root冲突 -->
   <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>springMVC4</param-value>
   </context-param>
 
   <!-- session timeout -->
   <session-config>
      <!-- 分钟 -->
      <session-timeout>60</session-timeout>
   </session-config>
 
   <!-- log4j配置 -->
   <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:/cfg/log/Log4j.properties</param-value>
   </context-param>
   <context-param>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>60000</param-value>
   </context-param>
   <!--添加监听 -->
   <listener>
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
   </listener>
   <!-- /log4j配置 -->
 
   <!-- welcome page -->
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
 
   <!-- session copy -->
   <distributable />
</web-app>
 

2.4 weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
   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/ejb-jar_3_0.xsd
   http://xmlns.oracle.com/weblogic/weblogic-web-app
   http://xmlns.oracle.com/weblogic/weblogic-web-app/1.2/weblogic-web-app.xsd">
 
   <container-descriptor>
      <!-- 针对weblogic10以下的版本使用该语句,不能与厦门的prefer-application-packages同时使用 -->
      <!-- <prefer-web-inf-classes>true</prefer-web-inf-classes> -->
      <!-- 针对weblogic10及以上的版本如果上面语句无法成功使用该语句,不能与厦门的prefer-web-inf-classes同时使用 -->
      <prefer-application-packages>
         <package-name>org.apache.commons.lang.*</package-name>
         <package-name>antlr.*</package-name>
         <package-name>org.hibernate.*</package-name>
         <package-name>javax.persistence.*</package-name>
      </prefer-application-packages>
   </container-descriptor>
</weblogic-web-app>
 

2.5spring-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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
 
   <!-- 自动扫描controller包下所有 -->
   <context:component-scan base-package="com.xqlee.springmvc.controller" />
   <!-- 注解驱动 -->
   <mvc:annotation-driven />
   <mvc:default-servlet-handler />
   <!-- 资源管理不拦截 -->
   <mvc:resources location="/resources/" mapping="/resources/**" />
   <mvc:resources location="/upload/" mapping="/upload/**" />
 
   <!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp -->
   <bean id="internalResourceViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 前缀 -->
      <property name="prefix" value="/WEB-INF/pages/" />
      <!-- 后缀 -->
      <property name="suffix" value="" />
   </bean>
 
   <!-- 处理在controller中使用注解@ResponseBody标签返回JSON数据 Start -->
   <!-- 注意[由于我在pom.xml引入的是2.4.1版本]:所以class=org.springframework.http.converter.json.MappingJackson2HttpMessageConverter -->
   <!-- [如果pom.xml引入的是1.xx版本]class=org.springframework.http.converter.json.MappingJacksonHttpMessageConverter -->
   <bean id="mappingJackson2HttpMessageConverter"
   class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
      <property name="supportedMediaTypes">
         <list>
            <value>text/html;charset=UTF-8</value>
            <value>text/json;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
         </list>
      </property>
   </bean>
   <bean
   class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
         <list>
            <ref bean="mappingJackson2HttpMessageConverter" />
         </list>
      </property>
   </bean>
   <!-- JSON数据处理 End -->
 
   <!-- 上传图片/文件需要配置 -->
   <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="-1" /><!-- -1表示没有限制 -->
      <property name="maxInMemorySize" value="4096" />
   </bean>
 
</beans>
 

2.6spring-beans.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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
              http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">
   <context:annotation-config />
   <!-- 启动自动扫描该包下所有的注解(如@controller) 一般扫描service,entity,dao,controller由springMVC扫描 -->
   <context:component-scan
      base-package="com.xqlee.springmvc.service,com.xqlee.springmvc.entity,com.xqlee.springmvc.dao" />
</beans>
 

2.7 Log4j配置文件

#defind the rooeLogger ,the rootlogger min level is INFO,and two way to output log
log4j.rootLogger = INFO,FILE,CONSOLE,DATABASE
 
#----------------------------------------------------------------------
#defind the FILE as file everyday                                     |
#----------------------------------------------------------------------
#log4j.appender.FILE = org.apache.log4j.DailyRollingFileAppender
 
#the log min level set as INFO
#log4j.appender.FILE.Threshold=INFO
 
#log file encoding set UTF-8
#log4j.appender.FILE.encoding=UTF-8
 
#log file path
#log4j.appender.FILE.File=/home/tomcat/log/springmvc4/springmvc4
 
#log file name append
#log4j.appender.FILE.DatePattern='_'yyyy-MM-dd'.log'
 
#Immediate write the log is true
#log4j.appender.FILE.ImmediateFlush=true
 
#the layout of the log
#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
 
#the detail layout of log  in the log file
#log4j.appender.FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} springmvc4 %-5p [%c:%L] | %m%n
 
#-------------------------------------------------------------------------
#this use the 'org.apache.log4j.ConsoleAppender' to output in the Console|
#-------------------------------------------------------------------------
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 
#output min level is INFO
log4j.appender.Threshold=INFO
 
#the output target is Console
log4j.appender.CONSOLE.Target=System.out
 
#the layout of the log
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} springmvc4 %-5p | %m%n
 
#----------------------------------------------------------------------------
#insert the log into database                                               |
#----------------------------------------------------------------------------
#log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
#The jdbc url
#log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/springmvc4
#The jdbc driver
#log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
#DB user
#log4j.appender.DATABASE.user=root
#DB password
#log4j.appender.DATABASE.password=root
#sql
#log4j.appender.DATABASE.sql=INSERT INTO zhl_log (operationTime,logLevel,logClass,logDetail)  VALUES ("%d{yyyy-MM-dd HH:mm:ss}", "%-5p","%F:%L", "%m")
#layout
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#layout detail
#log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
 

2.8测试vo

package com.xqlee.springmvc.vo;
 
/**
 *
 *
 * <pre>
 * @功能说明 [测试bean]
 * @其他说明 [暂无]
 * @文件编码 [UTF-8]
 * @所属包名 [com.xqlee.springmvc.vo]
 * @项目名称 [cdws]
 * @创建日期 []
 * @创建人员 [lxq]
 * @最后修改 []
 * @修改说明 [暂无更新]
 * @修改人员 [lxq]
 * @公司组织 [org]
 * @版本说明 [v 1.0]
 *
 * </pre>
 */
public class User {
 
   private String id;
   private String name;
   private String num;
 
   public User(String id, String name, String num) {
      this.id = id;
      this.name = name;
      this.num = num;
   }
 
   public String getId() {
      return id;
   }
 
   public void setId(String id) {
      this.id = id;
   }
 
   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }
 
   public String getNum() {
      return num;
   }
 
   public void setNum(String num) {
      this.num = num;
   }
 
}
 

2.9测试controller

package com.xqlee.springmvc.controller;
 
import java.util.Vector;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.xqlee.springmvc.vo.User;
 
/**
 *
 *
 * <pre>
 * &#64;功能说明 [测试类]
 * &#64;其他说明 [暂无]
 * &#64;文件编码 [UTF-8]
 * &#64;所属包名 [com.xqlee.springmvc.controller]
 * &#64;项目名称 [cdws]
 * &#64;创建日期 []
 * &#64;创建人员 [lxq]
 * &#64;最后修改 []
 * &#64;修改说明 [暂无更新]
 * &#64;修改人员 [lxq]
 * &#64;公司组织 [org]
 * &#64;版本说明 [v 1.0]
 *
 * </pre>
 */
@Controller(value = "/beanTestController.do")
public class TestController {
   /** 日志 **/
   private static final Log log = LogFactory.getLog(TestController.class);
 
   @RequestMapping(params = "method=Vector")
   // @ResponseBody
   public Object testVector(HttpServletRequest request, HttpServletResponse response) {
      // 创建容器
      Vector<User> vtb = new Vector<User>();
      User tb;
      for (int i = 0; i <= 5; i++) {
         tb = new User("ID-" + i, "小Name-" + i, "李Num-" + i);
         vtb.add(tb);
      }
      request.setAttribute("vtb", vtb);
      log.info("即将跳转.....");
      return "../../vector.jsp";
   }
}
 

2.10 index.jsp

<!DOCTYPE>
<html>
<body>
   <h2>Hello World!</h2>
   <a href="beanTestController.do?method=Vector">Vector Test</a>
</body>
</html>
 

2.11 vector.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE>
<html>
<body>
   <table border='1'>
      <thead>
         <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Num</td>
         </tr>
      </thead>
      <tbody>
         <c:forEach items="${vtb}" var="Object">
            <tr>
                <td>${Object.id}</td>
                <td>${Object.name}</td>
                <td>${Object.num}</td>
            </tr>
         </c:forEach>
      </tbody>
   </table>
 
 
</body>
</html>
 

2.12运行结果

3
4
 
暂无评论
站内搜索
搜索
广而告之(广告说明)
加入QQ群(641395244)
qq group
支持博客发展
support
推荐博客
暂无推荐
分享本文