springmvc之helloworld

来源:互联网 发布:京东淘宝竞品分析 编辑:程序博客网 时间:2024/06/05 00:50
<span style="color:#ff0000;"><strong>web.xml文件</strong></span>
<?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"><!-- 配置dispatcherServlet --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><span style="color:#ff0000;"><strong><!-- 配置dispatcherServlet的一个初始化参数:配置springmvc配置文件的位置和名称 实际上,也可以不通过contextConfigLocation来配置springmvc的配置文件,而是用默认的。默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml</strong></span><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param>--><!-- springDispatcherServlet是在当前web应用被加载的时候就被创建 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>


springmvc.xml-----加载springmvc配置的第一种方式:应用初始化参数

 

<?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.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><span style="color:#ff0000;"><strong><!-- 配置自动扫描的包 --></strong></span><context:component-scan base-package="com.atguigu.springmvc"></context:component-scan><span style="color:#ff0000;"><strong><!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 --></strong></span><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean></beans>

springDispatcherServlet-servlet.xml-----加载springmvc配置的第二种方式:默认配置文件

<?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.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><span style="color:#ff0000;"><strong><!-- 配置自动扫描的包 --></strong></span><context:component-scan base-package="com.atguigu.springmvc"></context:component-scan><span style="color:#ff0000;"><strong><!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 --></strong></span><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean></beans>


控制器,请求处理器Handler


package com.atguigu.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller//标识为控制器--请求处理器public class HelloWorld {<span style="color:#ff0000;"><strong>/** * 1.使用@RequestMapping这个注解来映射请求的URL * 2.返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下的解析: * 前缀prefix+returnVal返回值+后缀suffix---得到实际的物理视图,然后做--转发操作 *  * 例如本案例:/WEB-INF/views/success.jsp * @return */</strong></span><span style="color:#ff0000;"><strong>@RequestMapping("/helloworld")</strong></span>public String hello(){System.out.println("hello world");return "success";}}



视图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><a href="helloworld">hello world</a></body></html>



success.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><h4>您已经成功了hello world</h4></body></html>





特别注意:
<servlet-name>-servlet.xml
和 springmvc.xml任选泽其中一个即可。
选择springmvc.xml,需要在web.xml中配置
应用初始化参数,并且在src下面;
选择<servlet-name>-servlet.xml
不需要在web.xml中配置,但是其位置在WEB-INF下面



******************************************************************************************************************************************

这个是跟着尚硅谷佟刚的springmvc视频学习的。讲的很好,在此推荐给大家!!

******************************************************************************************************************************************

0 0
原创粉丝点击