引入jar包:
引入springmvc的jar包;
必须引入的jar包
- commons-logging.jar
- spring-aop.jar
- spring-beams.jar
- spring-context.jar
- sprign-core.jar
- spring-expression.jar
- spring-web.jar
- spring-webmvc.jar
在web.xml中配置dispatcherServlet
<!--开始·配置dispatcherServlet-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置dispatcherServlet初始化参数;用来配置springmvc配置文件的名称和位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
加入springMVC的配置文件
//声明控制器作用的包路径
<context:component-scan base-package="com.spring.mvc"></context:component-scan>
<!--视图解析器:将逻辑视图转为物理视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
编写处理请求的处理器,并标注为处理器
package com.laishuai.springmvc.text;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWord {
@RequestMapping("/hello")
public String sayHelloWord() {
System.out.println("helloWord");
return "success";
}
}
编写视图
//href的值为需要需要请求的mapping对象值
<a href="hello">hello</a>
网友评论