Maven SpringMVC项目基本搭建(备忘)
来源:互联网 发布:unity3d外包 编辑:程序博客网 时间:2024/06/16 17:33
简述:
可以把将要实现的MVC看做一个基于函数名的servlet的访问
如果使用ModelAndView就能实现页面跳转,但是我这里还是使用支持前台JS的异步调用形式,也就是返回一个JSON格式数据到前台,
步骤:
1. 新建Maven SpringMVC项目
按步骤新建
2. 新建之后如下图项目结构
删除 config包(这个是Maven构造项目时自动生成的,这里准备使用配置文件来加载)
3. 添加Gson包, 因为返回的是JSON格式数据所以需要, 有一个基类用来实现Response中返回数据
此时已经加入了Maven依赖项了
4. 构建web.xml
在这个xml配置文件中,吧关于mvc的servlet项加入到spring-mvc.xml中
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>SpringMVCProject</display-name><servlet><servlet-name>springServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext*.xml /WEB-INF/applicationContext*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><session-config><session-timeout>20</session-timeout></session-config></web-app>
5. 构建spring-mvc.xml
在这个文件中涉及到url的映射(在controller中的函数映射)
spring-mvc.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><value>/home=homeController</value></property><property name="order" value="1" /></bean><bean id="homeController" class="com.anialy.project.springmvc.controller.HomeController"> <property name="methodNameResolver" ref="paramResolver" /></bean><!-- 定义JSP --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean> <bean id="paramResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"><property name="paramName" value="action" /><property name="defaultMethodName" value="test" /></bean></beans>
6. 基类实现(现支持JSON的转换)
SpringMVCBaseController
package com.anialy.project.springmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.mvc.multiaction.MultiActionController;import com.google.gson.Gson;public class SpringMVCBaseController extends MultiActionController {private static Gson gson = new Gson();/** * 以文本形式下发数据 * @param response * @param responseText */protected void responseOutWithText(HttpServletResponse response,String responseText) {response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = null;try {out = response.getWriter();out.println(responseText);out.close();logger.debug("下发的数据是");logger.debug(responseText);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (out != null) {out.close();}}}/** * 以JSON格式输出 * @param response */protected void responseOutWithJson(HttpServletResponse response,Object obj) {response.setCharacterEncoding("UTF-8");response.setContentType("application/json; charset=utf-8");PrintWriter out = null;try {out = response.getWriter();String responseJsonStr = gson.toJson(obj);out.append(responseJsonStr);logger.debug("下发的数据是");logger.info(responseJsonStr);} catch (IOException e) {e.printStackTrace();} finally {if (out != null) {out.close();}}}}
7 Controller类实现
HomeController.java
package com.anialy.project.springmvc.controller;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;@Controllerpublic class HomeController extends SpringMVCBaseController{// 纯MVC 的视图界面返回//@RequestMapping(value="/")//public ModelAndView test(HttpServletResponse response) throws IOException{//return new ModelAndView("home");//}public void test(HttpServletRequest request, HttpServletResponse response) throws IOException{responseOutWithText(response, "Hello");}}
效果:
- Maven SpringMVC项目基本搭建(备忘)
- 三、maven项目搭建 springmvc+ spring + mybatis(springmvc搭建)
- Maven+SpringMVC+Mybatis项目搭建(一)
- Maven+SpringMVC+Mybatis项目搭建(二)
- Maven+SpringMVC+Mybatis项目搭建(三)
- Maven搭建SpringMVC项目详解
- SpringMVC项目搭建-maven版
- eclipse搭建maven SpringMVC项目
- Maven搭建SpringMVC+Mybatis项目
- Maven搭建SpringMVC+Hibernate4项目
- maven 搭建springmvc web项目
- Maven搭建SpringMVC项目详解
- 使用Maven搭建SpringMVC项目
- Maven快速搭建SpringMVC项目
- springMVC+mybatis+maven项目搭建
- maven 快速搭建 SpringMVC 项目
- 【SpringMVC】使用Maven搭建SpringMVC项目
- maven+springmvc+mybatis基本开发环境搭建
- 客户端通信过程
- 决定成败的49个细节
- Spring整合的quartz任务调度的实现方式
- SLF4J 教程(自由在各种log中切换)
- 直接拿来用!最火的iOS开源项目
- Maven SpringMVC项目基本搭建(备忘)
- 骑士人才系统后台用户名密码重设工具源码
- input子系统的基础知识
- wii homebrew 程序编程入门指南
- 黑马程序员:DVD管理系统项目之四--初识图形用户界面
- LinearLayout中有关CheckBox的处理
- fopen参数说明
- ffmpeg移植改接口调用 |
- CF 316C2(Tidying Up-二分图最大边权)