springMVC快速创建hello world

SpringMVC

1、配置springmvc的配置文件的头:

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:mvc="http://www.springframework.org/schema/mvc"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="
 6             http://www.springframework.org/schema/beans
 7             http://www.springframework.org/schema/beans/spring-beans.xsd
 8             http://www.springframework.org/schema/context
 9             http://www.springframework.org/schema/context/spring-context.xsd
10             http://www.springframework.org/schema/mvc
11             http://www.springframework.org/schema/mvc/spring-mvc.xsd">

maven中的pom配置文件中引入依赖:

 1  <properties>
 2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3     <maven.compiler.source>1.7</maven.compiler.source>
 4     <maven.compiler.target>1.7</maven.compiler.target>
 5     <!--版本锁定-->
 6     <spring.version>5.0.7.RELEASE</spring.version>
 7   </properties>
 8  9 <dependencies>
10   <dependency>
11     <groupId>org.springframework</groupId>
12     <artifactId>spring-context</artifactId>
13     <version>${spring.version}</version>
14   </dependency>
15 16   <dependency>
17     <groupId>org.springframework</groupId>
18     <artifactId>spring-web</artifactId>
19     <version>${spring.version}</version>
20   </dependency>
21 22   <dependency>
23     <groupId>org.springframework</groupId>
24     <artifactId>spring-webmvc</artifactId>
25     <version>${spring.version}</version>
26   </dependency>
27 28   <dependency>
29     <groupId>javax.servlet</groupId>
30     <artifactId>servlet-api</artifactId>
31     <version>2.5</version>
32   </dependency>
33 </dependencies>

2、使用时在web.xml中先配置 一个前端控制器(servlet),并将spring的配置文件装入该servlet中:(固定的)

 1 <servlet>
 2   <servlet-name>dispatcherServlet</servlet-name>
 3   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4   <init-param>
 5       <param-name>contextConfigLocation</param-name>
 6       <param-value>classpath:springMVC.xml</param-value>
 7   </init-param>
 8 </servlet>
 9 <servlet-mapping>
10   <servlet-name>dispatcherServlet</servlet-name>
11   <url-pattern>/</url-pattern>
12 </servlet-mapping>

3、SpringMVC中的controller层未采用servlet作为后端控制,而是使用

@Controller、@RequestMapping("Login")注解使一个Java类作为控制器。

补充:@RequestMapping(value="/")是将该方法映射成一个"servlet",通过url地址即可访问该方法。

1 @Controller
2 public class helloSpringMVC {
3     @RequestMapping(value = "/hello")
4     public String  sayhello(){
5         System.out.println("这是第一个springMVC程序");
6         return "success";
7     }
8 }

4、在spring的配置文件中,初始化视图解析器,用于处理针对return返回内容所跳转的页面

关键标签:InternalResourceViewResolver、

子标签:name="prefix"->跳转的页面的目录、name="suffix"->文件后缀名。

1 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
2     <property name="prefix" value="/pages/"></property> //跳转的页面目录
3     <property name="suffix" value=".jsp"/>//跳转的文件后缀名
4 </bean>
 

5、开启springMVC框架注解的支持

<mvc:annotation-driven/>
 

猜你喜欢

转载自www.cnblogs.com/lijie-helloworld/p/12449548.html