根据前面描述的工作流程,现在来对springmvc进行配置。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
创建springmvc的配置文件springmvc.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:tx="http://www.springframework.org/schema/tx" 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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
在web.xml中配置前端控制器。
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注意param-value中的路径无误,
在url-pattern中有两种配置方式:
第一种: *.action ,访问以.action结尾,由DispatcherServlet进行解析
第二种: \/ ,所有访问的地址由DispatcherServlet进行解析,对静态文件的解析需要配置不让DispatcherServlet进行解析,使用此种方式实现RESTful风格的url
错误配置: \/* ,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错404
配置处理器映射器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
配置处理器适配器
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
注解方式配置
<!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
<mvc:annotation-driven />
<bean name="/xxx.action" class="com.iot.ssm.controller.xxx"/>
注解批量扫描法
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.springdemo.*" />
配置视图解析器
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
property中prefix对应要查找的目录,suffix对应自动添加的后缀
测试:
controller:
package com.springdemo.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.View;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.springdemo.domain.User;
@Controller
@RequestMapping("/demo")
public class TestController implements org.springframework.web.servlet.mvc.Controller{
@RequestMapping("/demo")
public String index() {
return "index";
}
@Override
@RequestMapping("/test")
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
Map<String, Object> map=new HashMap<String, Object>();
User user=new User();
user.setName("梁非凡");
user.setId("123");
map.put("user", user);
String str="刘醒";
map.put("str", str);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addAllObjects(map);
modelAndView.setViewName("test");
return modelAndView;
}
}
test.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>
${user.name}Hbbbbbbbbb
</body>
</html>
配置到tomcat上,启动tomcat,打开浏览器,输入地址
http://localhost:8888/SpringMVCTest/demo/test
得到:
成功…