spring MVC环境搭建

来源:互联网 发布:淘宝达人的短视频教程 编辑:程序博客网 时间:2024/05/16 06:15

1、新建web项目,并在WEB-INFO下编写web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>springmvcdemo</display-name>  <listener>    <!-- 配置ContextLoaderListener,启动Web容器时,自动装配ApplicationContext的配置信息 -->    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 以通配符的方式,配置WEB-INF/classes下以spring-开头的xml文件 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/spring-*.xml</param-value>  </context-param>  <!-- 设置编码方式 -->  <filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <!-- 编码方式为utf-8 -->      <param-name>encoding</param-name>      <param-value>UTF8</param-value>    </init-param>    <init-param>      <!-- 强制覆盖编码方式为utf-8:意味着在代码中设置编码方式将失效 -->      <param-name>forceEncoding</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <!-- 编码的对象是,所有.do结尾的请求 -->    <filter-name>encodingFilter</filter-name>    <url-pattern>*.do</url-pattern>  </filter-mapping>  <!-- 配置前置控制器,拦截所有.do结尾的请求 -->  <servlet>    <servlet-name>mvcAction</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <!-- 这里的参数如果不配置,则默认查找web-inf下的{servlet-name}-servlet.xml文件 -->  <!-- <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/mvcAction-servlet.xml</param-value>  </init-param> -->  <servlet-mapping>    <servlet-name>mvcAction</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.html</welcome-file>  </welcome-file-list></web-app>

2、加入spring mvc的依赖jar包

    可根据项目的需要增减jar.

3、配置spring-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:mongo="http://www.springframework.org/schema/data/mongo"     xmlns:jms="http://www.springframework.org/schema/jms"     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"    xsi:schemaLocation="          http://www.springframework.org/schema/data/mongo          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd           http://www.springframework.org/schema/mvc           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd          http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd          http://www.springframework.org/schema/tx           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd          http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd          http://www.springframework.org/schema/jms           http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">    <!-- 配置一下常量到prop.properties,方便修改 -->    <context:property-placeholder location="classpath:prop.properties"/>    <!-- 标注自动扫描cn.jb.springmvc下的标有注解的类 -->    <context:component-scan base-package="cn.jb.springmvc"/>    <!--默认的mvc注解映射的支持 -->    <mvc:annotation-driven />    <!-- 事物管理器 -->    <tx:annotation-driven transaction-manager="txManager"  proxy-target-class="true"/>   <!--  <jms:listener-container connection-factory="mqConnectionFactory">        <jms:listener destination="${activemq.redisQueueName}" ref="redisMessageHandler" />        <jms:listener destination="${activemq.mongoDBQueueName}" ref="mongoDBMessageHandler" />        <jms:listener destination="${activemq.mySQLQueueName}" ref="mySQLMessageHandler" />        【注意】ref是需要注册的监听,destination是该监听所监听的队列,这里读取prop.properties中配置        <jms:listener destination="${activemq.demoYueEventQueueName}" ref="yueEventListener" />    </jms:listener-container>          映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射    <mongo:mapping-converter base-package="cn.tm" /> -->    <!-- SpringMVC上传文件时,需配置MultipartResolver处理器 -->      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <!-- 指定所上传文件的总大小不能超过800KB......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->          <property name="maxUploadSize" value="800000"/>      </bean>     <!-- 异常处理器,避免输出异常信息给用户 -->    <bean id="exceptionResolver" class="cn.tm.common.event.listener.ExceptionResolver"/>    <!--  拦截器 -->    <mvc:interceptors>          <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->          <bean class="cn.tm.common.event.listener.CommonInterceptor"/>          <!-- <mvc:mapping path="/test/number.do"/>              定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的              <bean class="com.host.app.web.interceptor.LoginInterceptor"/>           </mvc:interceptor>  -->    </mvc:interceptors>  </beans>

3、配置log4j.properties
这里写图片描述

4、配置mvcAction-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:p="http://www.springframework.org/schema/p" 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.0.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:property-placeholder location="classpath:prop.properties" />    <context:annotation-config />    <context:component-scan base-package="cn.tm.invite.web.action"></context:component-scan>    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <!-- jsp返回路径时的前缀 -->                <property name="prefix" value="/WEB-INF/page/"/>        <!-- jsp返回路径时的后缀 -->                <property name="suffix" value=".jsp"/>          </bean> </beans>

5、主要代码

package cn.tm.base.dao.mongoDB.impl;import java.util.ArrayList;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Sort;import org.springframework.data.domain.Sort.Direction;import org.springframework.data.geo.GeoResult;import org.springframework.data.geo.Point;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.NearQuery;import org.springframework.data.mongodb.core.query.Query;import org.springframework.stereotype.Repository;import cn.tm.base.dao.mongoDB.YueSystemEventMongoDBDAO;import cn.tm.base.model.UserInfo;import cn.tm.base.model.YueSystemEvent;import cn.tm.common.util.ConstantUtil;import cn.tm.common.util.PageUtils;/**  * 类描述:  * 创建人: * 作者单位: * 创建时间:2015-7-1 下午02:31:06  * @version:1.0 */@Repository("yueSystemEventMongoDBDAO")public class YueSystemEventMongoDBDAOImpl implements YueSystemEventMongoDBDAO{private static final String classname = YueSystemEvent.class.getSimpleName();    Logger logger = LoggerFactory.getLogger(getClass());    @Autowired    MongoTemplate mongoTemplate;    @Override    public void addYueSystemEven(YueSystemEvent yueSystemEvent){        mongoTemplate.save(yueSystemEvent, classname);    }    @Override    public List<YueSystemEvent> getYueSystemEventPage(String openid, String page, int yueSystemeventPagesize) {        Query query = new Query(Criteria.where("toOpenId").is(openid));        query.skip(PageUtils.getYueSystemEvenPageStartNum(Integer.valueOf(page))).limit(yueSystemeventPagesize);        query.with(new Sort(Direction.DESC, "createDate"));        List<YueSystemEvent> events = mongoTemplate.find(query, YueSystemEvent.class);        return events;    }}
package cn.tm.invite.web.action;import java.util.List;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import cn.tm.base.dao.mongoDB.YueSystemEventMongoDBDAO;import cn.tm.base.model.UserInfo;import cn.tm.base.model.YueSystemEvent;import cn.tm.common.ActionResult;import cn.tm.common.model.Message;import cn.tm.common.util.PageUtils;/**  * 类描述:  * 创建人:蒋斌 * 作者单位:tm * 创建时间:2015-7-1 下午05:24:22  * @version:1.0 */@Scope("prototype")@Controller@RequestMapping("/yueSystemEvent")public class YueSystemEventAction {    @Autowired    YueSystemEventMongoDBDAO yueSystemEventMongoDBDAO;    /**     * 用于ajax     * @param openid     * @param page     * @return     */    @RequestMapping("/getYueSystemEventPage")    public @ResponseBody    ActionResult getYueSystemEventPage(@RequestParam String openid, @RequestParam String page){        List<YueSystemEvent> events= yueSystemEventMongoDBDAO.getYueSystemEventPage(openid, page, PageUtils.YUE_SYSTEMEVENT_PAGESIZE);        return new ActionResult(1, Message.SUCCESS, events);    }    /**     * 范文jsp页面     * @param model     * @param session     * @return     */    @RequestMapping(value="/smessage")              public String smessage(Model model, HttpSession session){          String openid = (String)session.getAttribute("openid");        List<YueSystemEvent> events= yueSystemEventMongoDBDAO.getYueSystemEventPage(openid, "1", PageUtils.YUE_SYSTEMEVENT_PAGESIZE);        model.addAttribute("events", events);        return "smessage";              }}

5、总结
主要就是这些了,还有一些实体类,及接口代码没有贴上来。应为我用的不是mongodb所以没有配置事物管理器和数据源。这些知识在以后的学习中会写上来。

0 0