引入 Spring-Mvc 的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>``
- 1
- 2
- 3
- 4
- 5
配置两个 "器"
<!--配置注解扫描器-->
<context:component-scan base-package="com.it.bigdata"/>
<!–处理器映射器–>
<bean class=“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”></bean>
<!–处理器适配器–>
<bean class=“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”></bean>
<!–视图解析器–>
<!–<bean class=“org.springframework.web.servlet.view.InternalResourceViewResolver”></bean>–>`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
配置前端控制器
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
编写处理器
/**
* 处理器
*/
@Controller
public class MyController01 {
@RequestMapping("/func1.action")
public ModelAndView func1() {
System.out.println("func1..");
ModelAndView modelAndView = new ModelAndView("index02.jsp");
modelAndView.addObject("username", "admin");
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
编写视图
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>index02</title>
</head>
<body>
${username}
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-a47e74522c.css" rel="stylesheet">
</div>