SpringMVC入门

SpringMVC入门

大家好,又见面了,我是全栈君。

首先你要Spring全部的包导入

1、配置前端控制器DispatcherServlet 在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”>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring</servlet-name><!–src下[servletname]-servlet.xml中的servletname一一致–>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/spring-servlet.xml</param-value><!–默认DispatcherServlet会载入WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件->
</init-param>

<load-on-startup>1</load-on-startup><!–表示启动容器室初始化该servlet–>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern><!–表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。

也能够如“*.html”表示拦截全部以html为扩展名的请求。–>
</servlet-mapping>

</web-app>

2、建立一个HelloWorldController

package com.iss.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject(“message”, “helloworld”);
modelAndView.setViewName(“hello”);//设置视图名
return modelAndView;//返回模型数据和逻辑视图名
}

}

3、配置spring-servlet.xml

<?

xml version=”1.0″ encoding=”UTF-8″?>
<!– Bean头部 –>  
<beans  
    xmlns=”http://www.springframework.org/schema/beans”  
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”  
    xmlns:tx=”http://www.springframework.org/schema/tx”  
    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-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-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”>  

<!– HandlerMapping –>  
<bean class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”/>  

<!– HandlerAdapter –>  
<bean class=”org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter”/>

<!– ViewResolver –>  
<bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>  
    <property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/>  
    <property name=”prefix” value=”/WEB-INF/jsp/”/>  
    <property name=”suffix” value=”.jsp”/>  
</bean>
<!–处理器  –>
<bean name=”/hello” class=”com.iss.control.HelloWorldController”/> 
</beans>

<!–

InternalResourceViewResolver:用于支持Servlet、JSP视图解析。

 viewClass:JstlView表示JSP模板页面须要使用JSTL标签库。classpath中必须包括jstl的相关jar包;

 prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比方传进来的逻辑视图名为hello。则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”;

–>

4、在WEB-INF下建立jsp目录并新建一个hello.jsp文件注意hello已经在HelloWorldContoller中设置不能够任意命名

  <body>
${message } 
  </body>

5、在浏览器地址栏输入

http://localhost:8080/SpringMVC01/hello  页面出现helloworld!证明你成功了!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115449.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 字符串转List<map>「建议收藏」

    字符串转List<map>「建议收藏」List<byte[]>list=newArrayList<>();//近一周水汽热水能Map<String,Object>map=newHashMap<>();map.put(“1”,“A”);map.put(“2”,“B”);map.put(“3”,“C”);Map<String,Object>map2…

    2022年5月15日
    37
  • 请求头 content-type_详解中台

    请求头 content-type_详解中台敬请关注博客,后期不断更新优质博文,谢谢这里讲解Content-Type的可用值,以及在spring MVC中如何使用它们来映射请求信息。1. Content-Type MediaType,即是InternetMediaType,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。 例如:Content-…

    2022年8月24日
    11
  • offsetWidth与offsetLeft

    offsetWidth与offsetLeft1、offsetWidth:为元素的width+元素的padding+边框的宽度如图:2、offsetLeft、offsetTop、offsetRight、offsetBottom以offsetLeft为例进行说明,在不同的浏览器中其值不同,且与父元素的position属性(relative,absolute,fixed)有关。现分以下几种情况说明:(测试所用的浏览…

    2025年8月24日
    3
  • 磁盘初始化的过程

    磁盘初始化的过程1.低级初始化(物理初始化)1.1将磁盘分成扇区以便读写操作。1.2为每个扇区采用特别的数据结构2.磁盘分区将磁盘分为由一个或多个柱面组成的分区(C盘D盘)3.逻辑格式化3.1创建文件系统(根目录等)3.2将初始文件系统的数据结构存储到磁盘上(空闲、已分配的空间及一个空目录)…

    2022年9月21日
    4
  • A Game of Thrones(81)

    A Game of Thrones(81)45.EDDARD(0)Painisagiftfromthegods,LordEddard,”GrandMaesterPycelletoldhim.“Itmeanstheboneisknitting,thefleshhealingitself.Bethankful.”  “Iwillbethankfulwhenm…

    2022年8月23日
    60
  • github下载文件很慢(github打开慢)

    方法:更改hosts文件第一步:查询ip地址使用https://www.ipaddress.com/查询下面3个网站的ip地址github.comgithub.global.ssl.fastly.netcodeload.github.com2020.8.21查询结果如下(请勿直接复制):140.82.112.4github.com199.232.69.194github.global.ssl.fastly.net140.82.113.10codeload.github.com第二步

    2022年4月16日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号