springmvc一:编写简单的HelloWorld
生活随笔
收集整理的這篇文章主要介紹了
springmvc一:编写简单的HelloWorld
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1. 在web.xml中配置 DispatcherServlet.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 配置DispatcherServlet --><servlet><servlet-name>springDispatcherServlet</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><load-on-startup>1</load-on-startup></servlet><!-- /: 攔截所有請求,不攔截jsp頁面, *.jsp頁面/*: 攔截所有請求,攔截jsp頁面處理 *.jsp是tomcat做的事, 所有項目的小web.xml都是集成于大web.xmlDefaultServlet是tomcat中處理靜態資源的。除過jsp,servlet以外的都是靜態資源*.html就是靜態資源,tomcat就會在服務器下找到這個資源并返回。這里的前端控制器的/禁用了tomcat服務器的DefaultServlet--><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>2. 加入Spring MVC的配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- springmvc只是控制網站跳轉邏輯 --><!-- 只掃描控制器 --><context:component-scan base-package="com.atChina" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"/><property name="suffix" value=".jsp"/></bean><mvc:annotation-driven></mvc:annotation-driven><mvc:default-servlet-handler/> </beans>3. 編寫處理請求的處理器,并標識為處理器
package com.atChina.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class HelloWorld {/** 1. @RequestMapping: 映射請求的URL* 2. 返回值會通過視圖解析器解析為實際的物理視圖,對于InternalResourceViewResolver視圖解析器來說,* 會做如下的解析: 通過prefix+返回值+suffix的方式得到實際的物理視圖,然后做轉發操作* */@RequestMapping("/helloworld")public String hello(){System.out.println("Hello World");return "success"; // /WEB-INF/pages/success.jsp} }4. 編寫視圖
<%@ 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>你好啊 </body> </html>?
工程結構如下;
?
?web.xml也可以如下進行配置:(使用默認方式)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 配置DispatcherServlet --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 實際上也可以不通過contextConfigLocation來配置SpringMVC的配置文件,而使用默認的配置文件.默認的配置文件為: /WEB-INF/<servlet-name>-servlet.xml<init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param>--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>這時候的工程結構如下:?
總結
以上是生活随笔為你收集整理的springmvc一:编写简单的HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jvm十四:类的卸载
- 下一篇: 前端一HTML:十七:背景