【SpringMVC】概述

时间:2020-08-15 23:54:47   收藏:0   阅读:69

SpringMVC 概述

HelloWorld
步骤:

HelloWorld:加入 jar 包

jar 包:

HelloWorld:配置 web.xml

配置 DispatcherServlet :DispatcherServlet 默认加载 /WEB-INF/<servletName-servlet>.xml 的 Spring 配置文件, 启动 WEB 层 的 Spring 容器。可以通过 contextConfigLocation 初始化参数自定义配置文件的位置和名称。

<servlet>
  <servlet-name>helloworld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-mvc.xml</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>helloworld</servlet-name>
  <url-pattern>*.action</url-pattern>
</servlet-mapping>

HelloWorld:创建 Spring MVC 配置文件

HelloWorld:创建请求处理器类

@Controller
public class HelloWorld {
    @RequestMapping("/helloSpringMVC")
    public String helloworld() {
        System.out.println("helloworld...");
        //返回的字符串将会被视图解析器接收,加上前后缀变为视图路径
        return "success";
    }
}

web.xml

<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.action</url-pattern>
</servlet-mapping>

url

springmvc-1/helloWorld.action

Handler

@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public String helloWolrd(){
        System.out.println("HelloWorld SpringMVC");
        return "success";
    }
}

SpringMVC 配置文件

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name=“prefix” value=“/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

实际的物理视图

/WEB-INF/view/success.jsp
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!