springmvc基本工程搭建
在这里先给下我的建议:万事开头,思路最重要,理清思路,工作轻松、效率高。学的是思路,学的是进步!真实吧?
下面是springmvc工程搭建思路:
1、创建动态javaWeb工程
2、引入springmvc所需要的jar包及log4j.properties所需要的jar包
3、引入log4j.properties
4、创建一个controller包,一个Controller控制器类并写内容
5、创建一个jsp页面并写内容
6、创建springmvc.xml核心配置文件(controller包扫描,三大组件配置)
7、配置好web.xml(前端控制器:前端拦截器,加载springmvc.xml核心配置文件,映射器配置)
8、将项目添加到tomcat,启动tomcat
9、在谷歌浏览器测试
搭建实操
1、创建动态javaWeb工程
打开eclipse File>new>Dynamic Web Project,注意,这里要选择JavaEE企业版才有动态Web工程选项
2、引入springmvc所需要的jar包及log4j.properties所需要的jar包
点击链接获取:
springmvc https://pan.baidu.com/s/1PtA2pieCXXG-3uVLBNIKfw 提取码:9do3
log4j https://pan.baidu.com/s/1H40tXqneqry8-qwF8M3JpQ 提取码:0lic
3、引入log4j.properties(创建与src同级的资源目录config,创建log4j.properties,写入以下内容)
内容为:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4、创建一个controller包,一个Controller控制器类并写内容
package com.xue.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 这是一个测试类,测试springmvc搭建
* @author xuexue
*
*/
@Controller
public class TestController {
@RequestMapping("test")
public String Test(Model model) {
model.addAttribute("msg","Test is success!");
return "msg";
}
}
5、创建一个jsp页面并写内容(在WEB-INF下创建一个jsp文件夹,在jsp文件夹创建jsp页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
${msg }
</body>
</html>
6、创建springmvc.xml核心配置文件(controller包扫描,三大组件配置)(放置与log4j.properties同目录,config目录下)
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置controller扫描包 -->
<context:component-scan base-package="com.xue.controller" />
<!-- 配置处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!-- 配置处理器适配器-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 配置注解驱动,相当于同时使用最新处理器映射跟处理器适配器,对json数据响应提供支持 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7、配置好web.xml(前端控制器:前端拦截器,加载springmvc.xml核心配置文件,映射器配置)(WEB-INF下的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>springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- 前端拦截器配置 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 加载springmvc核心配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 映射器配置 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
最终项目结构为:
8、将项目添加到tomcat,启动tomcat
9、在谷歌浏览器测试
测试成功!可以正常开发了。当然,一般是整合spring,mybatis来开发。
项目下载地址:https://pan.baidu.com/s/1u0V5wJxsyTFp0kQhJ0qcFw 提取码:n16q