springmvc3.2 demo

系统 1392 0
1:mvn 构建一个web工程
[img]
springmvc3.2 demo
[/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;}

  

springmvc3.2 demo


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论