`
阅读更多
1:mvn 构建一个web工程
[img]

[/img]
resources 文件中定义系统资源文件包括applicationContext.xml 和dispatcher-servlet.xml 文件
java 文件中 是.java 文件
static 文件中包括 css img js
config .properties 文件
jsp中主要包含 系统个模块的试图文件
tags 是自定义标签文件
tiles 是模板文件
login.jsp 登陆页面

2:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cn</groupId>
  <artifactId>springMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springMVC</name>
  <url>http://maven.apache.org</url>
  <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.slf4j-version>1.6.1</org.slf4j-version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>3.2.0.RELEASE</version>
    	<exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
        </exclusions>
    </dependency>
    <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>3.2.0.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>3.2.0.RELEASE</version>
	</dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-expression</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aspects</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-test</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>javax</groupId>
    	<artifactId>javaee-web-api</artifactId>
    	<version>7.0</version>
    </dependency>
    <dependency>
    	<groupId>commons-collections</groupId>
    	<artifactId>commons-collections</artifactId>
    	<version>20040616</version>
    </dependency>
    <dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.3</version>
	</dependency>
    <!-- jack core annotation databind -->
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-core</artifactId>
    	<version>2.2.2</version>
    </dependency>
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-annotations</artifactId>
    	<version>2.2.2</version>
    </dependency>
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-databind</artifactId>
    	<version>2.2.2</version>
    </dependency>
    <dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-mapper-lgpl</artifactId>
		<version>1.9.12</version>
	</dependency>
    <!--  db mysql jdbc tx-->
    
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.25</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-tx</artifactId>
    	<version>3.2.0.RELEASE</version>
    </dependency>
    
	 <!-- Log slf4j log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
	<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
			<scope>runtime</scope>
	</dependency>

	<!-- apache tiles -->
	<dependency>
		<groupId>org.apache.tiles</groupId>
		<artifactId>tiles-extras</artifactId>
		<version>3.0.1</version>
	</dependency>
	
     <dependency>
     	<groupId>javax.servlet</groupId>
     	<artifactId>jstl</artifactId>
     	<version>1.2</version>
     </dependency>
  </dependencies>
  
  <build>
    <finalName>springMVC</finalName>
    <plugins>
    	<plugin>
    		<groupId>org.codehaus.mojo</groupId>
    		<artifactId>buildnumber-maven-plugin</artifactId>
    		<version>1.2</version>
    		<configuration></configuration>
    	</plugin>
    	<plugin>
    		<artifactId>maven-compiler-plugin</artifactId>
    		<version>3.1</version>
    		<configuration></configuration>
    	</plugin>
    	<plugin>
    		<artifactId>maven-war-plugin</artifactId>
    		<version>2.3</version>
    		<configuration></configuration>
    	</plugin>
    	<plugin>
    		<artifactId>maven-dependency-plugin</artifactId>
    		<version>2.8</version>
    		<configuration></configuration>
    	</plugin>
    	<plugin>
    		<groupId>org.codehaus.mojo</groupId>
    		<artifactId>tomcat-maven-plugin</artifactId>
    		<version>1.1</version>
    		<configuration></configuration>
    	</plugin>
    </plugins>
  </build>
</project>



3:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"   
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
         version="2.5" >  
    <display-name>springmvc</display-name>
    <context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/config/log4j.properties</param-value>
	</context-param>
	
    <!-- 区分项目名称,防止默认重名 -->  
    <context-param>  
        <param-name>webAppRootKey</param-name>  
        <param-value>com.cn.springmvc</param-value>  
    </context-param>  
    
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
    <!-- Spring的log4j监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    </listener>  
    
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
    <!-- 字符集 过滤器  -->  
    <filter>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    
    <filter-mapping>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern> 
        <dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher> 
    </filter-mapping>  
   <!-- 避免静态资源被dispatcher拦截,该servlet要定义在dispatcher前-->  
   <servlet-mapping>   
	    <servlet-name>default</servlet-name>  
	    <url-pattern>*.jpg</url-pattern>     
	</servlet-mapping>    
	<servlet-mapping>       
	    <servlet-name>default</servlet-name>    
	    <url-pattern>*.js</url-pattern>    
	</servlet-mapping>    
	<servlet-mapping>        
	    <servlet-name>default</servlet-name>       
	    <url-pattern>*.css</url-pattern>      
	</servlet-mapping>
	
    <!-- Spring view分发器 -->  
    <servlet>  
        <servlet-name>dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>2</load-on-startup>  
    </servlet>  
    
    <servlet-mapping>  
        <servlet-name>dispatcher</servlet-name>  
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
     
    <welcome-file-list>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>/WEB-INF/login.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>  


4:dispatcher-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"
		xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:oxm="http://www.springframework.org/schema/oxm"
		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.2.xsd
		        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
				http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
	
		
    <context:component-scan base-package="com.cn.ld.modules">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
	</context:component-scan>
	
	<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">-->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
		<property name="alwaysUseFullPath" value="true" />
		<property name="interceptors">
		    <list>
		       <ref bean="requestMappingHandlerMappingInterceptor" />
		    </list>
		</property>
	</bean>

	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<ref bean="mappingJacksonHttpMessageConverter" />
		</property>
	</bean>
	
	<bean id="requestMappingHandlerMappingInterceptor" class="com.cn.ld.framework.web.RequestMappingHandlerMappingInterceptor"/>
	
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
	
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.springframework.web.servlet.PageNotFound">error/pageNotFound</prop>
				<prop key="org.springframework.dao.DataAccessException">error/dataAccessFailure</prop>
				<prop key="org.springframework.transaction.TransactionException">error/dataAccessFailure</prop>
			</props>
		</property>
	</bean>    
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"
			p:suffix=".jsp" p:order="0"/>
			
</beans>




5:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<%= path%>/static/style/main.css" />
</head>
<body>
<form action="<%=path%>/user/login/doLogin" method="post">
<div><span class="span_width_60 text_algin_right">username:</span><span><input name="name"/></span></div>
<div><span class="span_width_60 text_algin_right">password:</span><span><input type="password" name="password"/></span></div>
<div><span class="span_width_60 text_algin_right">sex:</span><span><input type="radio" name="sex" value="男" checked="checked"/><input type="radio" name="sex" value="女"/></span></div>
<div><span class="span_width_60 text_algin_right">age:</span><span><input type="text" name="age" /></span></div>
<div><span class="span_width_60 text_algin_right"><input type="submit" value='submit'/></span></div>
</form>
</body>
</html>



6:jsp/user/home.jsp
<html>
<head>
<title>
</title>
<link rel="stylesheet" type="text/css" href="static/style/main.css" />
</head>
<body>
<div class="clear_float">
[list]
[*]username:${user.name}

[*]age:${user.age}

[*]sex:${user.sex}

[/list]
</div>
</body>
</html>


7: UserLoginController  controller 类
package com.cn.ld.modules.user.web;

import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/user")
public class UserLoginController {
	@RequestMapping("doLogin")
	public String query(Model model,User user) {
		model.addAttribute("user", user);
		return "home";
	}
}



8:RequestMappingHandlerMappingInterceptor 将 控制器注解 mapping 值 和 控制器返回的试图名 合并成试图的完整路径
package com.cn.ld.framework.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RequestMappingHandlerMappingInterceptor extends
HandlerInterceptorAdapter {

public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
mergeContollerMappingToView(modelAndView, handler);
}

/**
* 把view名称和controller @RequestMapping 值整合为一个新的路径
* @param mv
* @param handler
*/
protected void mergeContollerMappingToView(ModelAndView mv, Object handler)
{
if(mv == null)
return;

String vn=mv.getViewName();

if(vn == null)
return;

String cm=getControllerMapping(handler);

if(cm != null)
{
if(!cm.endsWith("/"))
cm+="/";

if(cm.startsWith("/"))
cm=cm.substring(1);

vn=cm+vn;
mv.setViewName(vn);
}
}

/**
* 获取controller @RequestMapping 值
* @param handler
* @return
*/
protected String getControllerMapping(Object handler)
{
HandlerMethod handlerMethod = (HandlerMethod) handler ;
Class<?> hc=handlerMethod.getBean().getClass();

RequestMapping mapping = AnnotationUtils.findAnnotation(hc, RequestMapping.class);

if(mapping != null)
{
String[] mvs=mapping.value();

re=mvs[0];
}


return re;
}
}

9: main.css
body{font-size: 12px ; font-family: 楷体;padding: 0px;width:1024px;min-height: 800px;}
.text_algin_right{text-align: right;}
.span_width_60{ display:-moz-inline-box; display:inline-block; width:60px;height:20px;padding: 0px;margin:0px; }
.span_width_20{ display:-moz-inline-box; display:inline-block; width:20px;height:20px;padding: 0px;margin:0px; }
.login_user{text-align: right;}

ul li
{ 
   list-style: none;
   margin-left: 15px;
   margin-bottom: 10px;
}
#menu
{
  clear:both;
  margin-bottom:5px;
  height:20px;
  background-color: gray;
}

#menu ul{
	list-style:none;
	height:auto;
	margin:0;p
	adding:0;
	background-color:#436973;
}

#menu li
{
    float:left;
	width:40px;
	height:20px;
}

#footer{background-color: gray;width: 100%;}
.clear_float{clear: both;}
  • 大小: 10.9 KB
2
1
分享到:
评论
5 楼 2047699523 2015-05-07  
spring mvc demo教程源代码下载:http://www.zuidaima.com/share/kspringmvc-p1-s1.htm
4 楼 minghua96 2014-01-24  
找了好久终于找打了 能把demo发一份给我吗 0546-110@163.com 谢谢
3 楼 zld406504302 2013-06-17  
jayung 写道
讨厌在jsp页面中出现java脚本片段,用EL、jstl看起来多舒服整洁,上下文用${pageContext.request.contextPath}或者先用<c:set var="ctx" value="${pageContext.request.contextPath}"/>,然后直接使
用${ctx}作上下文路径多简洁,再或者,定义一个application全局变量,页面直接表达式使用application变量更方便了。


有道理,谢谢指点.
2 楼 jayung 2013-06-17  
讨厌在jsp页面中出现java脚本片段,用EL、jstl看起来多舒服整洁,上下文用${pageContext.request.contextPath}或者先用<c:set var="ctx" value="${pageContext.request.contextPath}"/>,然后直接使
用${ctx}作上下文路径多简洁,再或者,定义一个application全局变量,页面直接表达式使用application变量更方便了。
1 楼 acw2000 2013-06-17  
找了好久终于找打了 能把demo发一份给我吗 war_speed@163.com 谢谢

相关推荐

    SpringMVC3.2 demo

    总的来说,"SpringMVC3.2 demo"项目展示了如何将SpringMVC与ZTree、JPA集成,构建一个具有基本登录功能的Web应用。通过学习这个项目,你可以了解到如何配置和使用这些技术,以及如何在实际开发中整合它们。同时,...

    springmvc demo

    Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发...

    SpringMVC3.2.0+MyBatis3.1.1+Spring3.2.0 整合

    Spring MVC的集成需要创建对应的包和控制器类(demo.controller),配置spring-mvc.xml以及在web.xml中设置DispatcherServlet。控制器类(如AreaController)负责接收HTTP请求,调用服务层的方法,然后返回响应。 ...

    springmvc-dubbo整合demo

    ### Spring MVC 与 Dubbo 整合知识点解析 #### 一、Spring MVC 与 Dubbo 概述 ...以上是对“springmvc-dubbo完整demo”的详细解析,希望能够帮助您更好地理解如何将 Spring MVC 与 Dubbo 进行有效整合。

    一个SpringMVCDemo

    Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,它是 Spring 框架的一部分,主要用于构建 MVC(Model-View-Controller)模式的 Web 应用程序。在 "一个SpringMVCDemo" 中,我们可以看到它被用于创建一个示例...

    ext-3.2.1 demo项目

    **EXT-3.2.1 Demo项目概述** EXT-3.2.1是EXT JavaScript库的一个版本,专门用于构建富互联网应用程序(RIA)。这个版本包含了丰富的用户界面组件和强大的数据管理功能,使得开发者能够创建出交互性强、视觉效果出色...

    SpringMVC 传送JSON数据到客户端

    SpringMVC通过@ResponseBody传送json字符串到客户端,实现了REST接口远程调用功能,demo中使用的版本为spring3.2,代码中用到了注解,代码直接部署运行,已经通过测试(如在浏览器中输入...客户端返回后台传递的json格式数据)

    SSM框架的demo

    spring4.3 + springMVC4.3 + mybatis3.2, mysql6.0, jdk1.8, maven3.5, eclipse4.5, 访问地址:localhost:端口号/news-web/test/findTest.do

    spring2.5+hibernate3.2

    spring2.5 + hibernate3.2x 标注(annotation)开发的简单示例 http://blog.csdn.net/IamHades/archive/2008/01/11/2038188.aspx

    基于SpingMvc i18n demo

    **SpringMvc i18n(国际化)Demo** 在Web开发中,为了使应用程序能够适应不同国家和地区的用户,实现多语言支持是至关重要的。SpringMvc框架提供了强大的国际化(i18n)功能,允许开发者轻松地根据用户的语言环境来...

    springboot3.2.3集成shardingsphere5.4.1及动态数据源demo项目

    《SpringBoot 3.2.3 集成 ShardingSphere 5.4.1 及动态数据源实战解析》 在现代企业级应用开发...通过这个Demo项目,开发者可以更好地理解和掌握这一集成方案,从而在实际项目中实现高效、灵活的数据管理和分片策略。

    shiro整合ssmDemo可直接运行

    #### 3.2 配置Shiro 创建一个`shiro.ini`或`shiro.xml`配置文件,定义Shiro的全局配置,如 Realm、缓存、过滤器链等。 Realm是Shiro与应用程序数据源交互的桥梁,通常我们需要自定义Realm以适应项目中的用户认证和...

    springboot开端学习基础 对springboot的基本认识和学习

    `INFO 6188 --- [ main] c.i.springboot.demo.HelloApplication : Started HelloApplication in 3.281 seconds (JVM running for 3.601)` 4. Spring Boot 的核心 4.1. 入口类和@SpringBootApplication Spring ...

Global site tag (gtag.js) - Google Analytics