spring MVC环境搭建

1、新建web项目,并在web.xml加入spring mvc的servlet

spring MVC环境搭建
<!-- spring mvc容器和servlet的定义 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 这里的参数如果不配置,则默认查找web-inf下的{servlet-name}-servlet.xml文件 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
spring MVC环境搭建

 

2、加入spring mvc的依赖jar包

spring MVC环境搭建spring MVC环境搭建

备注:我这里除了spring的jar包外还添加了一些其他的功能(quartz、JSR303的hibernate实现、c3p0连接池、DB2驱动、spring json的支持(jackson)),这里并未完全加入spring的包,可根据项目的需要增减jar.

 

3、配置spring mvc的配置文件。

 

spring MVC环境搭建
<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/mvc   
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <!-- Spring MVC配置 -->
    <context:annotation-config />
    <!--扫描注解 -->
    <context:component-scan base-package="com.tf" />
    <!--默认的mvc注解映射的支持 -->
    <mvc:annotation-driven/>
    <!-- 支持异步方法执行 -->
    <task:annotation-driven /> 

    <!-- 视图解析器和json解析器 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/jsp/" /> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
            </list>
        </property>
    </bean>
    
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="-1"/>
    </bean>

    <!-- 总错误处理 -->
    <bean id="exceptionResolver"
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView">
            <value>/error</value>
        </property>
        <property name="defaultStatusCode">
            <value>500</value>
        </property>
        <property name="warnLogCategory">
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
            </value>
        </property>
    </bean>

    <!-- 对静态资源文件的访问 -->
    <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" />
    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
    <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />


    <!-- 数据库和事务配置 -->

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 定义数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${jdbc.driverClass}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize">
            <value>${c3p0.minPoolSize}</value>
        </property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>${c3p0.maxPoolSize}</value>
        </property>
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>${c3p0.initialPoolSize}</value>
        </property>
        <!--每30秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>${c3p0.idleConnectionTestPeriod}</value>
        </property>
    </bean>
    <!-- weblogic推荐使用jndi连接池 -->
    <!-- 
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName"> 
            <value>java:comp/env/jdbc/myDatasource</value> 
        </property> 
    </bean> 
    -->

    <!-- 定义jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg><ref bean="dataSource"/></constructor-arg> 
    </bean>

    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="login*" propagation="REQUIRED" />
            <tx:method name="regist*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution (* com.tf.*.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    </aop:config>
</beans>
spring MVC环境搭建

 

4、配置log4j

spring MVC环境搭建
log4j.rootCategory=INFO, stdout , R   
   
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
log4j.appender.stdout.layout.ConversionPattern=[TF] %p [%t] %C.%M(%L) | %m%n   
    
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender   
log4j.appender.R.File=E:/mylog.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.Threshold = WARN 
log4j.appender.R.layout.ConversionPattern=%d-[TF] %p %t %c - %m%n   

#全局异常记录 log4j.logger.org.springframework.web.servlet.handler.SimpleMappingExceptionResolver=WARN
#项目的异常记录 log4j.logger.com.tf=ERROR
spring MVC环境搭建

 

5、编写测试代码

spring MVC环境搭建
package com.tf.model1.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.tf.model1.bean.User;
import com.tf.model1.service.TestService;

@Controller
//@RequestMapping(value="/test")
public class TestRestController {
    @Resource
    private TestService testService;
    
    //接收get请求
    @RequestMapping(value="/test",method=RequestMethod.GET)
    //可以自动根据名称映射参数,也可以手动指定
    public String test(@RequestParam(value="name",defaultValue="admin")String username,HttpServletRequest request){
    //public String test(@RequestParam("name")String username,HttpServletRequest request){
    //public String test(String name,HttpServletRequest request){
        //获取到spring的applicationContext对象
        WebApplicationContext context = RequestContextUtils.getWebApplicationContext(request);
        
        System.out.println("-------------------> name:" +username);
        return "test";
    }
    
    //redirect/forward:url方式转到另一个Action进行连续的处理
    @RequestMapping("/redirect")
    public String testRedirect(){
        return "redirect:/index.jsp";
    }
    
    //重定向并携带参数
    @RequestMapping("/redirectAttribute")
    public String testRdirectWithAttribute(RedirectAttributes attributes){
        attributes.addAttribute("name", "zhangsan");
        return "redirect:/login.jsp";
    }
    
    //上传1
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file,RedirectAttributes attributes){
        System.out.println("param:"+name);
        if(!file.isEmpty()){
            System.out.println("upload ok");
        }
        
        attributes.addAttribute("name", "upload success");
        return "redirect:/login.jsp";
    }
    
    //接收ajax请求
    //使用url的表达式动态传递参数
    //返回json
    @RequestMapping("/uri/{userId}")
    //多个url参数可以用多个注解和方法参数来接收
    public @ResponseBody Map uriTemplate(@PathVariable String userId){
    //也可以这么写
    //public @ResponseBody Map uriTemplate(@PathVariable("userId") String loginUserId){
        System.out.println("url的参数为:"+userId);
        Map<String, String> map = new HashMap<String, String>();
        map.put("user", userId);
        return map;
    }
    
    //测试另外一种json方式
    @RequestMapping(value="/nextjson",produces="application/json")
    //写在方法上还是返回参数前效果一致
    //@ResponseBody
    public @ResponseBody String testNextJson(){
        return "json";
    }
    
    //调用业务类执行保存操作
    @RequestMapping(value="/save",produces="application/json")
    public @ResponseBody Map<String, String> testSave(){
        this.testService.save();
        Map<String, String> map = new HashMap<String, String>();
        map.put("isOk", "success");
        return map;
    }
    
    //spring使用hibernate的valid进行数据校验
    @RequestMapping("/login")
    public String testValid(@Valid User user, BindingResult result){
        if (result.hasErrors()){
            List<ObjectError> errorList = result.getAllErrors();
            for(ObjectError error : errorList){
                System.out.println(error.getDefaultMessage());
            }
        }
           
        return "test";
    }
}
spring MVC环境搭建
spring MVC环境搭建
package com.tf.model1.bean;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;

import com.tf.common.db.PK;
import com.tf.common.db.Table;

/**
 * 
@Null
限制只能为null
@NotNull
限制必须不为null
@AssertFalse
限制必须为false
@AssertTrue
限制必须为true
@DecimalMax(value)
限制必须为一个不大于指定值的数字
@DecimalMin(value)
限制必须为一个不小于指定值的数字
@Digits(integer,fraction)
限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction
@Future
限制必须是一个将来的日期
@Max(value)
限制必须为一个不大于指定值的数字
@Min(value)
限制必须为一个不小于指定值的数字
@Past
限制必须是一个过去的日期
@Pattern(value)
限制必须符合指定的正则表达式
@Size(max,min)
限制字符长度必须在min到max之间
 * 
 * @author yzl
 *
 */

@Table("T_USER")
public class User {
    @PK
    private String userId;
    @NotNull(message="名字不能为空")
    private String userName;
    @Max(value=120,message="年龄最大不能查过120")
    private int age;
    @Email(message="邮箱格式错误")
    private String email;

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
spring MVC环境搭建

 <mvc:annotation-driven />的具体说明:

 <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。

<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

spring MVC环境搭建,布布扣,bubuko.com

spring MVC环境搭建

上一篇:javascript系列之核心知识点(一)


下一篇:初学java之触发响应事件举例子