首页 > 学院 > 开发设计 > 正文

Spring MVC学习

2019-11-08 20:07:38
字体:
来源:转载
供稿:网友

搭建SPRing Mvc步骤

1:创建WEB项目

2:导入Spring Mvc jar包

3:配置web.xml文件

<servlet>   <servlet-name>mvc</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <servlet-mapping>   <servlet-name>mvc</servlet-name>   <url-pattern>*.do</url-pattern>  </servlet-mapping>

4:配置servletname—servlet.xml

<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 配置handerAdapter  适配器 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>   <bean id="jspViewResolver" 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"/>   <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面--></bean><!-- spring容器扫描指定包下的所有类,如果类上有注解  那么根据注解产生相应bean对象已经映射信息 --><context:component-scan base-package="cn.cjz.spring"/></beans>

5:编写代码

@Controllerpublic class hello {@RequestMapping("/hi.do")public ModelAndView hello(){ModelAndView mv = new ModelAndView();mv.setViewName("hello");mv.addObject("msg", "hello");return mv;}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表