2.2 maven基础上搭建springmvc4.X,一步一步超简单。

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.0modelVersion>
  <groupId>mavenSpringMybatisDemogroupId>
  <artifactId>mavenSpringMybatisDemoartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>mavenSpringMybatisDemo Maven Webappname>
  <url>http://maven.apache.orgurl>



  <properties>
      <spring.version>4.0.2.RELEASEspring.version>
      <junit.version>4.11junit.version>
 properties>


  <dependencies>
    
      <dependency>
              <groupId>junitgroupId>
              <artifactId>junitartifactId>
              <version>3.8.1version>
              <scope>testscope>
      dependency>
      <dependency>
              <groupId>com.sun.facesgroupId>
              <artifactId>jsf-apiartifactId>
              <version>2.0.4-b09version>
      dependency>
      <dependency>
              <groupId>com.sun.facesgroupId>
              <artifactId>jsf-implartifactId>
              <version>2.0.4-b09version>
      dependency>
      <dependency>
              <groupId>javax.servletgroupId>
              <artifactId>jstlartifactId>
              <version>1.2version>
      dependency>
      <dependency>
              <groupId>taglibsgroupId>
              <artifactId>standardartifactId>
              <version>1.1.2version>
      dependency>

    <dependency>
       <groupId>junitgroupId>
       <artifactId>junitartifactId>
       <version>${junit.version}version>
      dependency>
      <dependency>
       <groupId>org.springframeworkgroupId>
       <artifactId>spring-webmvcartifactId>
       <version>${spring.version}version>
      dependency>
    dependencies>
  <build>
    <finalName>mavenSpringMybatisDemofinalName>
  build>
project>

3.修改web.xml




<web-app> 
  <description>最简单的SpringMVC环境抢建description>  
    
  <context-param> 
    <param-name>contextConfigLocationparam-name>  
    <param-value>classpath:/spring/applicationContext.xmlparam-value> 
  context-param>  
    
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 
  listener>  
    
  <servlet> 
    <servlet-name>myspringmvcservlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
      
    <init-param> 
      <param-name>contextConfigLocationparam-name>  
      <param-value>classpath:/springmvc/springmvc-servlet.xmlparam-value> 
    init-param> 
  servlet>  
    
  <servlet-mapping> 
    <servlet-name>myspringmvcservlet-name>  
    <url-pattern>*.htmlsurl-pattern> 
  servlet-mapping>  
  <welcome-file-list> 
    <welcome-file>index.jspwelcome-file> 
  welcome-file-list> 
web-app>

4.添加springmvc配置文件

根据web的配置,在src/main/resources文件夹下新建springmvc配置文件springmvc/springmvc-servlet.xml
内容:



<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">  
    
  <mvc:annotation-driven/>  
    
  <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>.jspvalue> 
    property> 
  bean> 
beans>

5.增加spring配置

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




<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"%> 
 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Insert title heretitle> 
head> 
    <body> Page1 body> 
html>

8.修改index

修改index.jsp

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

9.目录结构

![这里写图片描述]
2.2 maven基础上搭建springmvc4.X,一步一步超简单。_第1张图片

10.启动eclipse下的tomcat

访问http://localhost:8080/mavenSpringMybatisDemo/
2.2 maven基础上搭建springmvc4.X,一步一步超简单。_第2张图片
2.2 maven基础上搭建springmvc4.X,一步一步超简单。_第3张图片
搭建不出来请留言

11.demo下载地址

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

ps

下一面配置spring

你可能感兴趣的:(java技术)