【SpringMVC】快速入门(注解版本)(十)
程序员文章站
2022-07-15 11:10:44
...
1)springmvc快速入门(传统版)
@Controller:表示当前类是控制类
@RequestMapping(value="/hello")只能方法的访问映射地址,可以省略.action
步一:创建springmvc-day02这么一个web应用
步二:导入springioc,springweb和springmvc相关的jar包
------------------------------------------------------springWEB模块
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(mvc专用)
------------------------------------------------------springIOC模块
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
步三:在/WEB-INF/下创建web.xml文件
<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:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
步四:创建HelloAction.java控制器类
@Controller
public class HelloAction{
@RequestMapping(value="/hello")
public String helloMethod(Model model) throws Exception{
System.out.println("HelloAction::helloMethod()");
model.addAttribute("message","这是我的第二个springmvc应用程序");
return "/success.jsp";
}
}
步五:在/WebRoot/下创建success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>这是我的第二个springmvc应用程序</title>
</head>
<body>
success.jsp<br/>
${message}
</body>
</html>
步六:在/src/目录下创建spring.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:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- Action控制器 -->
<context:component-scan base-package="cn.lfsenior.javaee.springmvc.helloannotation"/>
<!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
步七:部署web应用到tomcat中,通过浏览器访问如下URL:
http://127.0.0.1:8080/springmvc-day02/hello.action
上一篇: springMVC注解之入门
推荐阅读
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
JAVA WEB快速入门之从编写一个基于SpringMVC框架的网站了解Maven、SpringMVC、SpringJDBC
-
SpringMVC快速入门,这一篇就够了
-
springMvc入门 三.Scope注解与@RequestParam注解
-
SpringMVC快速入门(XML版本)
-
SpringMVC入门学习三--注解方式
-
springMVC注解之入门
-
【SpringMVC】快速入门(注解版本)(十)
-
springMVC注解入门
-
SpringMVC入门使用注解