SpringMVC4.0 + Tomcat7 + JDK7环境搭建 + (Spring4.0jar包+源码+logging+SpringIDE百度网盘下载)

来源:互联网 发布:可可网络验证后台 编辑:程序博客网 时间:2024/05/19 16:35

今天一时兴起想用一下新版本的框架,就找了一个SpringMVC4.0的来,还是遇到一些问题,写下来帮助一下大家吧,程序员都知道配环境是最头痛的。

这个里面就是Spring4.0jar包+源码+logging+SpringIDE,如果能下载别忘了点个赞。
云盘链接

链接:http://pan.baidu.com/s/1c1XqZOs 密码:y26a

最最基本的目录结构如下:
这里写图片描述

先建立动态web工程,然后导入jar包,把SpringMVC几个必须的jar包,加上logging处理日志jar包复制到lib即可。在目录结构图也可以看到。

然后就是配置web.xml
web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>springmvc1</display-name>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <servlet>        <servlet-name>dispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置DispatcherServlet的一个初始化参数:配置SprngMVC配置文件的位置和名称 -->        <!--             实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,而使用            默认的配置文件:/WEB-INF/<servlet-name>-servlet.xml         -->         <!--         <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>         -->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

配置好web.xml之后,我们就要实现那个servlet的配置文件了,可以用<init-param>来控制,也可以用默认的配置。
默认配置的话就要注意命名规则:<servlet-name>-servlet.xml
dispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">    <!-- 指定要扫描的包 -->    <context:component-scan base-package="com.hust.springmvc1"></context:component-scan>    <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>

然后编写控制器,用注解的方式
HelloWorld.java

package com.hust.springmvc1;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {    /**     * 使用@RequestMapping注解来映射请求的URL     * @return     */    @RequestMapping("/helloworld")    public String hello() {        System.out.println("hello");        return "success";    }}

然后就是页面了,index页面不用说吧。
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body>    <a href="helloworld">Hello</a></body></html>

注意,因为配置的时候success.jsp放在WEB-INF/views下,那么这个就要放在这个底下,配置文件也可以自行更改,那么这个也要改。
success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body>    <h1>success...</h1></body></html>

这个时候启动服务器。就会进入index.jsp页面,点击那个超链接,如果成功就会进入success…页面,那么到此你的SpringMVC就helloworld了。

2 0
原创粉丝点击