springMVC学习-day01
生活随笔
收集整理的這篇文章主要介紹了
springMVC学习-day01
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. springMvc組件與流程
2.代碼編寫
<%@ 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><a href="springmvc/testRequestMapping">test RequestMapping</a><br /><br /><a href="helloworld">helloworld</a> </body> </html> package com.zgz.springmvc.handlers;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;//標識這個類是一個控制器類 @Controller public class helloWorld {/*** 1. 使用@RequestMapping()注解來映射請求的url* 2. 返回值會通過視圖解析器解析為實際的物理視圖, 對于InternalResourceViewResolver 視圖解析器會做如下解析: * 通過prefix(前綴) + returnVal + suffix(后綴)的方式得到實際的物理視圖* @return*/@RequestMapping("/helloworld")public String hello() {System.out.println("hello world!");return "success";} } <?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"version="3.0"><!-- 配置 DispatcherServlet --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置 DispatcherServlet 的一個初始化參數:配置 SpringMvc 配置文件的位置和名稱--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 當springDispatcherServlet第一次被加載的時候就創建而不是第一次請求是才創建 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app> <?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 配置自動掃描的包 --><context:component-scan base-package="com.zgz.springmvc.handlers"></context:component-scan><!-- 配置視圖解析器: 如何把 handler方法返回值解析為實際的物理視圖 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean> </beans> <%@ 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><h3>hello world</h3> </body> </html>轉載于:https://blog.51cto.com/13416247/2096188
總結
以上是生活随笔為你收集整理的springMVC学习-day01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lvs和keepalives
- 下一篇: 在Linux安装配置Tomcat 并部署