`

SpringMVC小示例

阅读更多
***因为在内网环境下,依赖的jar不能从网上直接下载,所以没有采用maven,此博客只为记录Spring+jsp所依赖的最小jar包集合。***

一、配置web.xml,主要作用
1.定义了一个servlet拦截器,以及Mapping规则
2.引入spring-context配置文件
3.添加log4j配置文件
具体文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SpringTest</display-name>
	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/resources/log4j/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


二、DispatcherServlet Context配置文件,作用
1.激活注解配置功能
2.配置资源文件路径
3.配置视图层技术
4.引入具体组件配置文档
具体文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven>

	</annotation-driven>

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<!-- Imports user-defined @Controller beans that process client requests -->
	<beans:import resource="controllers.xml" />


	
</beans:beans>


三、组件配置文档,主要作用
1.配置默认页面路径
2.配置组件扫描路径
具体文件
<?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.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- Maps '/' requests to the 'home' view -->
	<mvc:view-controller path="/" view-name="views/hello.jsp"/>
	
	<context:component-scan base-package="test.springmvc"/>
	 
</beans>


四、controller代码
/**
 * 
 */
package test.springmvc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/mvc")
public class MCVController {
	private static final Log logger = LogFactory.getLog(MCVController.class);
	@RequestMapping("/hello")
	public String hello(){
		logger.info("start to maping request hello");
        return "hello";
    }
	
	@RequestMapping(value = "/test",method =RequestMethod.GET) 
	public @ResponseBody String test(){
        return "abc";
    }
	
	@RequestMapping(value = "/test2",method =RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	public @ResponseBody  AbcTest test2(){
		AbcTest test = new AbcTest();
		test.setAge("20");
		test.setName("me");
        return test;
    }
}



jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
  
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <div style = "width: 200px;height: 100px;color: red">hello</div>
   <button id='buttontest' style = "width: 50px;height: 50px;color: blue"></button>
   <div>
        <label>test:</label>
         <span id="test"></span>
   </div>
   <div>
        <label>test2:</label>
        <span id="test2"></span>
   </div>
  </body>
</html>

<script src="resources/js/jquery-2.0.3.js"></script>
<script type="text/javascript">
$('#buttontest').click(function() {
	$.ajax({
        type: "GET",
        url: "mvc/test",
        /* headers:{
            Accept: "text/plain"
        }, */
        success: function(msg){
          console.log( "Data Saved: " + msg );
          $("#test").text(msg);
        },
        error:function(e){
            alert(e)
        }
     });
 
 $.ajax({
        type: "GET",
        url: "mvc/test2",
        success: function(msg){
          console.log( "Data Saved: " + msg );
          $("#test2").text("age:"+msg.age+",name"+msg.name);
        },
        error:function(e){
            alert(e)
        }
     });
})

</script>


最终的工程目录:


工程代码可参考附件
  • 大小: 91.7 KB
分享到:
评论

相关推荐

    Java进阶Spring和springMVC详细示例精通教程资料.7z

    传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料

    SpringMVC代码示例

    **SpringMVC 框架详解** SpringMVC 是 Spring 框架...提供的 "spring-mvc-study" 压缩包可能包含了一系列的 SpringMVC 示例代码,可以帮助进一步理解这些概念和用法。通过深入学习和实践,你可以掌握这个强大的框架。

    springmvc登陆示例项目

    本项目“springmvc登陆示例项目”是针对初学者和开发者的一个实践教程,旨在帮助他们理解并掌握SpringMVC在处理用户登录场景中的应用。 该项目名为"goodDay",可能代表一个简单的日常登录系统,其核心功能是实现...

    SpringMVC示例

    这个"SpringMVC示例"是展示如何使用SpringMVC来实现基本的后台接口服务。通过理解SpringMVC的工作原理,开发者可以有效地创建可扩展且易于维护的后端系统。 在SpringMVC中,核心组件包括DispatcherServlet、...

    SpringMVC demo 完整源码实例下载.zip

    在示例中,我们可能会看到Service层和DAO层的实现,它们封装了业务逻辑和数据库操作。 文件上传和下载功能在很多Web应用中不可或缺。SpringMVC提供MultipartFile接口处理文件上传,而文件下载则可以通过...

    Shiro+SpringMVC 示例

    Apache Shiro 和 SpringMVC 的整合示例是一个用于构建安全Web应用的常见实践。这个示例项目不依赖于任何数据库,所有的实现都是基于静态代码,这使得它成为一个快速理解和学习Shiro安全框架的理想起点。 Apache ...

    activeMQ+spring+springmvc整合示例

    这个"activeMQ+spring+springmvc整合示例"旨在帮助开发者理解如何将这三者结合,以实现消息队列(Message Queue)的功能,提高系统的异步处理能力和负载均衡。以下是关于这一整合的详细知识讲解: 1. **ActiveMQ**:...

    springmvc框架示例源代码

    在这个“springmvc框架示例源代码”中,我们可以深入理解Spring MVC的核心概念和工作流程。 首先,Spring MVC 的核心组件包括DispatcherServlet、Controller、Model、ViewResolver以及各种HandlerMapping和...

    springMVC示例

    总结来说,这个 "springMVC示例" 是一个学习 Spring MVC 的良好起点,涵盖了创建 Controller、处理请求、使用 Model、配置视图解析器以及在 Servlet 中获取 Spring Bean 的基本概念。通过这个示例,开发者可以逐步...

    springmvc入门示例

    在压缩包文件列表中的 "springmvc" 文件可能包含了项目结构,包括 `src/main/java` 目录下的源代码、`src/main/resources` 目录下的配置文件,以及 `pom.xml` 或 `build.gradle` 文件用于构建项目。初学者可以按照...

    swagger集成springMVC简单示例

    在这个"swagger集成springMVC简单示例"中,我们将探讨如何将Swagger整合到Spring MVC框架中,以便自动化地生成API文档,提高开发效率。 首先,Swagger的核心组件是`Swagger-core`,它提供了注解,允许我们在Java...

    SpringMVC入门示例demo 程序源码

    springmvc_demo_02是非注解模式,springmvc_demo_03是手动指定springmvc的核心配置文件的位置的,springmvc_demo_04是使用springMVC的注解模式 csdn原文博客:...

    springMVC图片上传示例

    在这个"springMVC图片上传示例"项目中,我们将深入探讨如何在SpringMVC环境中实现图片附件的上传功能,包括多图上传、图片预览及文本域传值。 一、SpringMVC概述 SpringMVC是Spring框架的一部分,它遵循模型-视图-...

    SpringMVC官方示例

    这个“SpringMVC官方示例”是Spring官方提供的一个实例项目,旨在帮助开发者更好地理解和掌握SpringMVC的核心功能和用法。在本示例中,你将能够学习到如何配置SpringMVC、如何处理HTTP请求、如何定义控制器、以及...

    SpringMVC例子代码小汇总

    **SpringMVC简介** SpringMVC是Spring框架的一部分,它是一个用于构建Web应用程序的轻量级、模型-视图-...提供的压缩包"SpringMVC"可能包含了一些示例代码,这些代码可以帮助初学者更好地理解和学习SpringMVC的使用。

    Maven+SpringMVC+Mybatis项目搭建示例完整版

    基于Maven搭建Spring+Mybatis的项目,包含Maven聚合和继承、Mybatis增删改查和分页、SpringMVC开发及使用拦截器检查登录等知识点。详细介绍 http://blog.csdn.net/autfish/article/details/52037354

Global site tag (gtag.js) - Google Analytics