1、新建Maven工程,将Eclipse调制J2EE模式,porm.xml文件中添加spring-webmvc依赖;
2、在工程/src/webapp/web-inf/web.xml中输入配置:
<?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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>CaoPorn1</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet在初始化方法里面,
会读取该初始化参数的值,来获得spring配置
文件的位置,然后启动spring容器 -->
<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>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
告诉服务器spring.xml文件的位置与名字,spring.xml文件放在src/main/resources下,具体代码为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 这里可以处理各种请求,后期窄化之后分配url简单一点 -->
<prop key="/hello.do">UserController</prop>
<prop key="/login.do">UserController</prop>
</props>
</property>
</bean>
<!-- 配置处理器 -->
<bean id="UserController" class="com.CaoPorn.Controller.UserController"></bean>
<!-- 开启接口扫描
<aop:config proxy-target-class="false"></aop:config> -->
<!-- 开启spring的注解 主因在上面生命mvc的空间 和引入xsd文件
<mvc:annotation-driven/>-->
<!-- 访问静态资源
<mvc:default-servlet-handler/> -->
<!-- 启用spring mvc 注解
<context:annotation-config /> -->
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com"
></context:component-scan>
<!-- 对转向页面加前后缀,视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=""></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
接下来开始在src/main/java中写代码,包名com.xxxxx.controller为了日后扫包时方便,新建Controller类,具体代码为:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class UserController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("hellospring");
System.out.println(request.getRequestURI());
return new ModelAndView("hello");
}
}
在 src/main/webapp下新建hello.jsp
这里将hello传给视图解析器,视图解析器加前后缀进行跳转,跳转至hello.jsp