IDEA

2018-03-26  本文已影响11人  黑夜与繁星

SpringMVC 概述:

image
  1. 新建项目。
  2. 添加 SpringMVC 及 web 到项目中。
  3. 定义项目名。
image image
  1. 修改 Tomcat 名称。
  2. 部署服务器。
  3. 添加部署地址。
image image

将红框中的 jar 包文件添加到左面编译区。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
dispatcher-servlet.xml
```wd

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"> 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        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">
 6     <!-- 扫描自定义包 -->
 7     <context:component-scan base-package="com.itdoc.springmvc"/>
 8  
10     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11         <property name="prefix" value="/WEB-INF/views/"/>d
12         <property name="suffix" value=".jsp"/>
13     </bean>
14 </beans></pre>

<?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"
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">

<context:component-scan base-package="com.itdoc.springmvc"/>

<!-- 配置试图解析器: 把 Controller 返回值解析成实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

</beans>


HelloWorld.java

[![复制代码](https://img.haomeiwen.com/i1852846/f5232082a4c3105f..gif?imageMogr2/auto-orient/strip)](javascript:void(0); "复制代码") 

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"> 1 package com.itdoc.springmvc; 2 
 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 
 6 /**
 7 * @BLOG http://www.cnblogs.com/goodcheap
 8  * @DESCRIBE
 9 * @AUTHOR WángChéngDá 10 * @DATE 2017-03-08 12:19 11  */
12 @Controller 13 public class HelloWorld { 14 
15     private final static String SUCCESS = "success"; 16 
17     /**
18 * 1.使用 @RequestMapping 注解来映射请求的 URL。 19 * 2.返回值会通过试图解析器解析为实际的物理视图, InternalResourceViewResolver 试图解析器是 20 * 通过 prefix + returnVal + suffix 这样的方式得到实际的物理视图, 然后做转发操作。 21 * 22 * /WEB-INF/views/ + success + .jsp 23 * 24 * @return
25      */
26     @RequestMapping("/helloworld") 27     public String hell() { 28         System.out.println("hello world!"); 29         return SUCCESS; 30 } 31 }</pre>

package com.itdoc.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**

上一篇下一篇

猜你喜欢

热点阅读