1.前言

1.本博客面向初级开发者。

2.本博客为系列博客,《1.X》系列为服务器数据库相关技术,前几章为简单搭建linux+tomcat+MySQL+nginx+redis、《2.X》为java后台技术前几章为搭建mevan+springmvc4+mybatis、《3.X》为前端UI技术,《4.X》为相关测试技术。

3.本博客在学习别人博客技术基础上,并加以自己实现研究优化为全部原创,有什么问题可以留言,不定期回答,互相交流学习。

2.搭建springmvc

1.搭建maven

相关文档:http://blog.csdn.net/zcjfzcjf/article/details/51105522^_^
下载地址:http://download.csdn.net/detail/zcjfzcjf/9486599^_^

2.修改pom文件,增加jar。

<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>mavenSpringMybatisDemo</groupId>
  <artifactId>mavenSpringMybatisDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenSpringMybatisDemo Maven Webapp</name>
  <url>http://maven.apache.org</url>


<!--   声明版本 -->
  <properties>
      <spring.version>4.0.2.RELEASE</spring.version>
      <junit.version>4.11</junit.version>
 </properties>


  <dependencies>
    <!--    maven包 -->
      <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
      </dependency>
      <dependency>
              <groupId>com.sun.faces</groupId>
              <artifactId>jsf-api</artifactId>
              <version>2.0.4-b09</version>
      </dependency>
      <dependency>
              <groupId>com.sun.faces</groupId>
              <artifactId>jsf-impl</artifactId>
              <version>2.0.4-b09</version>
      </dependency>
      <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
      </dependency>
      <dependency>
              <groupId>taglibs</groupId>
              <artifactId>standard</artifactId>
              <version>1.1.2</version>
      </dependency>
<!--       springjar包 -->
    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>${junit.version}</version>
      </dependency>
      <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>${spring.version}</version>
      </dependency>
    </dependencies>
  <build>
    <finalName>mavenSpringMybatisDemo</finalName>
  </build>
</project>

3.修改web.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app> 
  <description>最简单的SpringMVC环境抢建</description>  
  <!-- 加载Srping配置文件 -->  
  <context-param> 
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:/spring/applicationContext.xml</param-value> 
  </context-param>  
  <!-- Spring 监听 -->  
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>  
  <!-- SpringMVC配置 -->  
  <servlet> 
    <servlet-name>myspringmvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <!-- 自定义springmvc的配置文件名称和路径 -->  
    <init-param> 
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:/springmvc/springmvc-servlet.xml</param-value> 
    </init-param> 
  </servlet>  
  <!-- springmvc 请求后缀 -->  
  <servlet-mapping> 
    <servlet-name>myspringmvc</servlet-name>  
    <url-pattern>*.htmls</url-pattern> 
  </servlet-mapping>  
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>

4.添加springmvc配置文件

根据web的配置,在src/main/resources文件夹下新建springmvc配置文件springmvc/springmvc-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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
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 
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  <!-- 激活@Controller模式 -->  
  <mvc:annotation-driven/>  
  <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->  
  <context:component-scan base-package="com.my1.controller"/>  
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
    </property>  
    <property name="suffix"> 
      <value>.jsp</value> 
    </property> 
  </bean> 
</beans>

5.增加spring配置

根据web的配置,在src/main/resources文件夹下新建spring配置文件spring/applicationContext.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans></beans>

6.新建demo控制层代码

在src/main/java下
新建com.my1.controller.DemoController.java
内容:

package com.my1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DemoController {
 @RequestMapping(value = "/toPage1.htm")
 public ModelAndView toPage1() {
  return new ModelAndView("page1");
 }
}

7.在WEB-INF目录下新建jsp/page1.jsp

1.WEB-INF在src/main/webapp下
2.在WEB-INF目录下新建jsp/page1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="utf-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Insert title here</title> 
</head> 
    <body> Page1 </body> 
</html>

8.修改index

修改index.jsp

<html>
<body>
<h2>Hello World!</h2>
<a href="toPage1.htmls">To Page1</a>
</body>
</html>

9.目录结构

![这里写图片描述]
这里写图片描述

10.启动eclipse下的tomcat

访问http://localhost:8080/mavenSpringMybatisDemo/
这里写图片描述
这里写图片描述
搭建不出来请留言

11.demo下载地址

http://download.csdn.net/detail/zcjfzcjf/9500980
如有问题请留言

ps

下一面配置spring

Logo

更多推荐