星空网 > 软件开发 > Java

简单的SpringMVC经典案例

主题:构建一个基于SpringMVC的HelloWord Web 项目

目的:快速体验什么是SpringMVC

方案

  1、创建工程,命名:SpringMVC

  简单的SpringMVC经典案例

  2、导包

  简单的SpringMVC经典案例

  3、在SRC下添加spring-mvc.

   (注意:名字可以随便取,最好就是看上就知道是什么)

  简单的SpringMVC经典案例

  

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.springframework.org/schema/mvc" ="http://www.w3.org/2001/  ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/aop"  xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd"></beans>

 

  4、在web.

<??><web-app ="http://www.w3.org/2001/ ="http://java.sun.com/ xsi:schemaLocation="http://java.sun.com/ id="WebApp_ID" version="2.5"> <display-name>SpringMVC</display-name> <servlet>   <servlet-name>SpringMVC</servlet-name>   <!-- DispathcherServlet 前端控制器 -->   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <init-param>     <!-- 变量名随便取 -->     <param-name>contextConfigLocation</param-name>     <!-- 指定SpringMVC配置文件名 -->     <param-value>classpath:spring-mvc.</param-value>   </init-param>   <!-- load-on-startup等于1,则表示容器启动就实例化此Servlet -->  <load-on-startup>1</load-on-startup>    </servlet> <servlet-mapping>   <!-- 要与上面Servlet的名字对应 -->   <servlet-name>SpringMVC</servlet-name>   <!-- 用来匹配客户端请求 -->   <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>

 

  5、在spring-mvc.

                       【InternalResourceViewResolver组件】--作用------>设置视图配置

                                               【HelloController】------------------------作用------->测试请求处理

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.springframework.org/schema/mvc" ="http://www.w3.org/2001/  ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/aop"  xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd">  <!-- 定义客户端请求映射关系 -->  <!-- HeanlerMapping是Spring核心组件之一 -->  <bean id="headlerMapping"      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">     <property name="mappings">       <map>         <entry key="/hello.action">           <value>helloController</value>         </entry>       </map>     </property>  </bean>    <!-- 增加HelloController的Bean -->  <bean id="helloController" class="controller.HelloController" />    <!-- 定义视图解释器(Spring核心组件之一) -->  <bean id="viewResolver"      class="org.springframework.web.servlet.view.InternalResourceViewResolver">     <property name="prefix" value="WEB-INF/jsp/"/>     <property name="suffix" value=".jsp"/>  </bean></beans>

 

  6、编写HelloController【注意:需要实现Controller接口

package controller;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 HelloController implements Controller{  @Override  public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {    ModelAndView mv = new ModelAndView("hello");    System.out.println("处理hello.action请求");    return mv;  }  }

  7、在WEB-INF文件夹下新增"jsp"文件夹,并添加hello.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>  欢迎来到Spring的世界!</body></html>

  8、跑起来吧兄弟们~然后访问http://localhost/SpringMVC/hello.action,效果如下:

  简单的SpringMVC经典案例

  简单的SpringMVC经典案例

 




原标题:简单的SpringMVC经典案例

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

中国跨境电商有哪些:https://www.goluckyvip.com/tag/49163.html
中国跨境电商展:https://www.goluckyvip.com/tag/49165.html
中国跨境电子商务专业委员会:https://www.goluckyvip.com/tag/49166.html
中国跨境物流fba:https://www.goluckyvip.com/tag/49167.html
中国联塑:https://www.goluckyvip.com/tag/49168.html
中国零售电商报告:https://www.goluckyvip.com/tag/49169.html
23点聊电商:新质生产力加速数字贸易发展 卓尔智联集团实现营收利润双增长 :https://www.kjdsnews.com/a/1836411.html
南京浦口都有什么好玩的地方 南京浦口都有什么好玩的地方推荐:https://www.vstour.cn/a/363180.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流