作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程。最近学习一下spring web mvc,写下我个人的一些经验。

1。准备jar包。spring mvc 已经到了版本4,网上的很多资料已经不在适用。给出的下载地址也无法适用。这是非常痛苦的。我费了好大功夫才获得这些JAR包。

在官方介绍文档里获得http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#overview-distribution-zip

在这里下载:http://repo.spring.io/release/org/springframework/spring/4.0.1.RELEASE/

同时还需要jstl包,你可以搜索到。

2. 创建一个web project项目,将jar包放入工程的WEB-INF的lib下。如下图:

3,创建 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Web Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/spring/spring-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/spring/spring-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

  

4.创建文件夹WEB-INF/conf/spring,再创建文件spring-servlet.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置SpringMVC配置文件所需的xml文件解析模板 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置控制器要掃描的包路徑 -->
<context:component-scan base-package="zyf.demo" />
<!-- 配置視圖解析器 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 解析器解析/WEB-INF/jsp/路徑下,以.jsp結尾的視圖文件 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

5.写控制器

package zyf.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/demo")
public class HelloWorldController { @RequestMapping("/helloWorld")//url字段名
public String helloWorld() {
return "helloWorld";//jsp文件名
}
}

6.写 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">
<%
request.getRequestDispatcher("/demo/helloWorld.htm").forward(request, response);
%>

大功告成。中间更多的时间浪费在找jar包问题上。

最新文章

  1. 关于GIL
  2. 操作系统win2003 x64的,安装OFFICE2003后,DCOM服务找不到 WORD应用程序服务
  3. java提高篇(二五)-----HashTable
  4. 用c#操作Mongodb(附demo)
  5. KM模板
  6. jquery取消选择select下拉框
  7. VS2010 发布web项目 问题
  8. c语言,strcat(),字符串拼接
  9. (转载)PHP数组传递是值传递而非引用传递
  10. UIScrollView的基本使用和一些常用代理方法
  11. 对Qt for Android的评价(很全面,基本已经没有问题了,网易战网客户端就是Qt quick写的),可以重用QT积累20年的RTL是好事,QML效率是HTML5的5倍
  12. Mahout分布式运行实例:基于矩阵分解的协同过滤评分系统(一个命令实现文件格式的转换)
  13. git常用命令行总结
  14. [android] 采用layoutInflater打气筒创建一个view对象
  15. CentOS 6.5优化开机启动服务
  16. vscode settings.json
  17. 决策单调性优化dp
  18. PHP多进程处理并行处理任务实例(转,备用)
  19. 安装Qt5.9
  20. 微信小程序云开发

热门文章

  1. python标准库介绍——37 signal 模块详解
  2. DataGridView控件使用大全说明-各种常用操作与高级操作
  3. U811.1接口EAI系列之一-通用访问EAI方法--统一调用EAI公共方法--VB语言
  4. 学习Git---20分钟git快速上手
  5. MySql(十三):MySql架构设计——可扩展性设计之 MySQL Replication
  6. android AlertDialog设置点击button不退出(转)
  7. Requests: 让 HTTP 服务人类
  8. STL学习笔记— —无序容器(Unordered Container)
  9. ssh以密钥的方式登录服务器时,只要有密钥可以登服务器,如果有密钥和公钥同时存在(在公钥没问题的情况下可以),但如果公钥有问题,就不能登录成功
  10. (原创)C++11改进我们的程序之简化我们的程序(五)