一个简单的springmvc程序
1、@Controller
@RequestMapping("/hello")
这就是传说中的spring。
controller是一个顶层的结构
requestMapping是一个映射的地址
下图是:
HelloController.java代脂轿码
2、下图是hello.jsp页面
在Java代码中我们可以科昆找到message
model.addAttribute("message", "Hello Spring MVC Framework!");
在jsp页面我们也可以找到message
这就是它们的映射关系
3、<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="Com.spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
beans是头文件
context是包文件
bean是路径
property是在/WEB-INF/jsp/之下的jsp文件
4、<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.4">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是一个项目的映射关系。
项目各糟薪名:HelloWeb
spring路径:
org.springframework.web.servlet.DispatcherServlet
load-on-startup 元素标记容器是否起动加载
url-pattern路径访问方式
5、如果我们的代码没有错,就可以起动项目,如果在下面console中没有报错,说明我们的代码是没有什么问题的,就可以在访问路径中修改一下。
@RequestMapping("/hello")
http://localhost:8080/HelloWeb/hello
6、测试成功,说明代码没有什么问题。