Spring-MVC初学篇
准备工作:
新建动态网页项目

引入Spring-MVC的jar包:http://repo.spring.io/release/org/springframework/spring/
引入依赖jar包:
Commons-logging:http://commons.apache.org/proper/commons-logging/download_logging.cgi
复制jar包到lib目录,在Build Path中引入。

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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>zol</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>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置参数 --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
配置参数详解:
<param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value>
contextConfigLocation:
此参数代表该servlet的配置文件位置,如果未配置,默认为 WEB-INF/(servlet-name)-servlet.xml。
classpath:
代表(项目发表后)/WEB-INF /classes/这个路径。
常用的场景: 在SSH架构中,配置Spring的上下文环境:
<!-- Spring Configures --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param>
classpath:applicationContext.xml可用/WEB-INF /classes/ applicationContext.xml代替。
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件。
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
spring-servlet.xml配置代码
<?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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 默认扫描包 --> <context:component-scan base-package="controller"></context:component-scan> <!-- 静态资源处理 --> <mvc:default-servlet-handler /> <!-- 注解驱动 --> <mvc:annotation-driven /> <!-- 视图处理 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
命名空间的查找:(xmlns:perfix=“······”) perfix即是替代的前缀
Spring需要用到的jar包会有对应的文件,文件中有对应的命名空间。Libaraies下找到引用的jar包,双击打开目录。图中指向的即是命名空间及其前缀。
配置文件的查找:xsi:schemaLocation="{namespace} {location}" {namespace}是前面找到的那段,{location}是下图标记的那段。引号内括号隔开。

在src下新建controller包
在controller包里面新建class文件
controllers.java
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/wap") public class controllers { @RequestMapping("/index") public String index(HttpServletRequest request,HttpServletResponse response) { System.out.println("to index"); return "copy"; } }
此时只要在WEB-INF文件下新建index.jsp 和 copy.jsp
即可通过http://localhost:8080/zol/访问主页index和http://localhost:8080/zol/wap/index/访问copy页。

浙公网安备 33010602011771号