SpringMVC环境搭建及Hello World

1.导包

此过程可以导入相关SpringMVC的jar包,也可以直接搭建Spring环境进行这一步骤的操作
我用的myeclipes进行傻瓜式导包
在这里插入图片描述

2.配置web.xml

	<servlet>
	<servlet-name>springMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	<servlet-name>springMVC</servlet-name>
	<url-pattern>/</url-pattern>
	</servlet-mapping>

3.配置-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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.1.xsd">
 
    <!-- 扫描组件,将加上@Controller注解作为SpringMVC控制器 -->
    <context:component-scan base-package="com.baidu.www" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 
 
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
 
</beans>

4.定义一个控制器类

package com.baidu.www;

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

@Controller
public class TestController {

	@RequestMapping(value="hello")
	public String hello() {
		System.out.println("success");
		return "success";//视图名称
		
	}
}


5.创建一个success.jsp

在/WEB-INF/view/目录下创建一个success.jsp

6.测试

在浏览器输入localhost:8080/projectName/
会直接跳转到index.jsp
我在jsp中写了

 <a href="hello">Test</a>

当点击超链接的时候,就会跳转到localhost:8080/projectName/hello,当controller类接收到请求时进行处理,根据return返回值,进行相应的页面跳转即:localhost:8080/projectName/前缀/返回值.后缀==localhost:8080/projectName/前view/success.jsp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值