步骤:
a) 导入相关jar包
a) 导入相关jar包
b)配置web.xml文件-----配置分发器
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMvc01</display-name>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<span style="color:#006600;"><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>*.do</url-pattern>
</servlet-mapping></span>
</web-app>
c)添加springmvc的配置文件 默认在WEB-INF下添加【DispatcherServletName】-servlet.xml文件
d)编写controller类
public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(
HttpServletRequest paramHttpServletRequest,
HttpServletResponse paramHttpServletResponse) throws Exception {
String username = paramHttpServletRequest.getParameter("username");
System.out.println("username:" + username);
ModelAndView mav = new ModelAndView("index");
//封装要显示视图的数据
mav.addObject("hello", username);
return mav;
}
}
e)编写SpringMVC配置文件
<?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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置HandlerMapping 根据beanname找对对应的controller -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>
<!-- 配置controller -->
<bean name="/hello.do" class="cn.com.jit.controller.HelloController"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/login/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>