在学习Springmvc进行WEB开发时,我们首先需要创建一个WEB项目(废话)。
之后需要编写Springmvc的Controller类:
@Controller:声明控制器
@RequestMapping("/helloWorld"):声明URL
@RequestMapping("/hello1"):声明二级URL
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
@RequestMapping("/hello1")
public String hello1(){
System.out.println("hellspringmvc1");
return "/welcome.jsp";
}
@RequestMapping("/hello2")
public String hello2(){
System.out.println("hellspringmvc2");
return "/welcome.jsp";
}
}
在编写好了Controller控制器类之后记得写一个jsp页面用来测试跳转(也就是上面的"welcome.jsp")。
因为是框架,所以我们还必须要写出Springmvc的xml配置文件:
最上面把文本格式改成"UTF-8"方便编辑。
给配置文件加入context标签属性,就是这三句:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.controller"/>
</beans>
加入context标签之后使用context标签中的如下语句来对"base-package"所指定的包进行扫描,其主要目的是扫描控制器类。
<context:component-scan base-package=""/>
最后用Myeclipse自带的tomcat跑一下,大功告成!