作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程。最近学习一下spring web mvc,写下我个人的一些经验。
1。准备jar包。spring mvc 已经到了版本4,网上的很多资料已经不在适用。给出的下载地址也无法适用。这是非常痛苦的。我费了好大功夫才获得这些JAR包。
在这里下载:http://repo.spring.io/release/org/springframework/spring/4.0.1.RELEASE/
同时还需要jstl包,你可以搜索到。
2. 创建一个web project项目,将jar包放入工程的WEB-INF的lib下。如下图:
3,创建 web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?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包问题上。