搭建SpringMVC的框架
创建一个web项目
导入包
配置web.xml文件
<!-- 配置springDispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springDispatcherServlet的初始化参数 spring配置文件的名称和位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- springmvc.xml在src文件夹下面 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
SpringMVC的HelloWorldDemo
配置springmvc.xml文件
<!-- 配置自定义扫描的包 -->
<context:component-scan base-package="com.com.springmvc.handler"></context:component-scan>
<!-- 配置视图解析器 把Controller的方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
在index.jsp文件中编写一个请求
<a href="helloworld">helloworld</a>
创建一个控制器
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
/**
* 使用@RequestMapping()注解来映射请求的URL
* 通过springmvc.xml文件中的
* <property name="prefix" value="/views/"></property>
* <property name="suffix" value=".jsp"></property>
* index.jsp中的helloworld请求对应@RequestMapping("/helloworld")
可以得到返回到实际的物理视图的页面是WebContent/views/success.jsp 文件
* @return
*/
@RequestMapping("/helloworld")
public String hello(){
System.out.println("this is HelloWorld class hello method");
return "success";
}
}
创建WebContent/views/success.jsp
部署运行服务,点击index.jsp页面上的超链接,就会转到success.jsp。