搭建方式:一、配置形式
1、web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>SpringMVCDemo02</display-name>
<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*:applicationContext_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
load-on-startup元素标记容器是否在启动的时候就加载这个servlet
1)它的值必须是一个整数,表示servlet应该被载入的顺序
2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
5)当值相同时,容器就会自己选择顺序来加载。
DispatcherServlet:解析请求映射的找到application-mvc.xml
SimpleUrlHandelMapping:建立请求和action或者controller之间的映射
2、
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 由IOC管理表现层实现类 -->
<bean id="testAction" class="com.javalxj.spring.mvc.action.TestAction"></bean>
<!-- 定义返回页面相关参数 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property><!-- 前缀 --> <property name="suffix" value=".jsp"></property><!-- 后缀 --> </bean> <!-- 建立URL映射 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.do">testAction</prop> </props> </property> </bean>
package com.javalxj.spring.mvc.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class TestAction implements Controller{ /** * 默认方法 */ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub System.out.println("testAction"+request.getParameter("userName")); return new ModelAndView("index"); } }
二、注解形式
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 建立请求与类的映射--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫描模块找到有标记的类:把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="com.javalxj.spring"></context:component-scan> <!-- 定义返回页面相关参数 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property><!-- 前缀 --> <property name="suffix" value=".jsp"></property><!-- 后缀 --> </bean>
@Controller
public class TestController { //用于处理响应 @RequestMapping("/login.do") public String login(String userName,String userPwd,Map map){ if("admin".equals(userName)&&"123".equals(userPwd)){ map.put("userName",userName); System.out.println("testController delete mathod:"+userName+":"+userPwd); return "success"; } System.out.println("testController delete mathod:"+userName+":"+userPwd); return "login";