SpringMVC框架之HelloWorld
来源:互联网 发布:淘宝网超溥长款羽戎服 编辑:程序博客网 时间:2024/06/05 04:21
一、SpringMVC数据请求流程
request –> DispatcherServlet –>Handler Mapping –>Control Servlet –>ModelAndView –>ViewResolver –> View –> response
其中 ModelAndView 会调用 数据层(db,cache….)此处略
二、创建一个第一个springMVC工程
(环境搭建此处略)
工程说明;
该工程主要是一个简单的交互,用户输入一个字符串,提交后将在另一个页面展示
1、用eclipse创建一个动态的Web工程, 在生成的时候注意勾选上自动生成web.xml文件,系统默认不自动生成
2、将Spring所需要的jar包copy到lib目录下。 (spring包可在相关网址下载)
3、在web.xml配置 DispatchServlet与HandlerMapping的名字。
配置如下:
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>test1</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>test1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <!-- 配置Handler Mapping --> <servlet-mapping> <!-- 注意 Bean Mapping必须在文 件 test1-servlet.xml在进行栩置 --> <servlet-name>test1</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
由于我的ServletName配置的是test1,那么必须新增一个test-servlet.xml文件。
该文件在最后列出,因为还有其它的配置需要说明。
4、创建hello.jsp页面,如下
响应事件为 hello.do, hello.do如何与控制器关键是在test1-servlet.xml中定义实现的
<%@ 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><form action="hello.do" method="post"> hello:<input name="hello" type="text" /> <input type="submit" value="提交" /></form></body></html>
创建Controller, 如下所示
使用了最原始的方式(非注解),该控制器主要获取 “hello” 参数,然后再返回给index页面,实际是index.jsp 因为在test1-servlet配置页面解析器时,与置了后缀
package com.test1.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class HelloController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { String hello = arg0.getParameter("hello"); System.out.println("arg0 = " + hello); ModelAndView mav = new ModelAndView("index"); mav.addObject("helloworld", "hello:" + hello); return mav; }}
创建view/index.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><h1>${helloworld }</h1></body></html>
注意:index.jsp必须在view,原因是因为在test1-servlet中配置了前置器为 /view/ 这意味首后续路径均以 /view/ 为根目录
test1-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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-3.0.xsd"> <!-- 固定配置,配置解年方式 --> <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"> </bean> <!-- Bean 映身配置 --> <bean name="/hello.do" class="com.test1.Controller.HelloController"></bean> <!-- 页面解析器配置,配置前置解析器,后置解析器,主要用于路径拼节 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
此至,一个简单的helloworld工程完成。
- SpringMVC框架之HelloWorld
- 【springmvc】 springmvc 之 HelloWorld
- springmvc之HelloWorld
- 剖析springmvc之HelloWorld
- springmvc之helloworld
- SpringMVC之HelloWorld
- SpringMVC实例之HelloWorld
- SpringMVC之“HelloWorld”起步
- springmvc之helloworld
- SpringMVC之HelloWorld。
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- 学习SpringMVC(一)之helloworld
- springMVC web工程之 HelloWorld
- SpringMVC开发之HelloWorld案例以及分析
- 工具类
- vivado(1)——创建工程
- SimpleDateFormat类时间转换BUG
- a'a
- 前端面试大全
- SpringMVC框架之HelloWorld
- Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/
- 第一个spring boot示例
- HR教你如何判断一个公司的好坏?
- 文本文件编辑相关命令(二)删除,移动,复制
- js 中{},[]中括号,大括号使用详解
- spring 源码分析及知识点总结
- Mybatis 查询 or 语句
- 递归—汉诺塔