设为首页 收藏本站
查看: 1160|回复: 0

[经验分享] spring MVC环境搭建

[复制链接]

尚未签到

发表于 2017-2-19 11:38:09 | 显示全部楼层 |阅读模式
  1、新建web项目,并在web.xml加入spring mvc的servlet



<!-- 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>
  2、加入spring mvc的依赖jar包
DSC0000.jpg DSC0001.jpg

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



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
  5、编写测试代码



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";
}
}


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;
}
}
<mvc:annotation-driven />的具体说明:
   <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。
  <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-344197-1-1.html 上篇帖子: 《程序人生》:分享一个女程序员的故事 下篇帖子: (一)开篇—杂谈WebGIS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表