一、SpringMVC下载

在Spring官网下载Spring Framework

http://www.springsource.org/spring-framework

二、在MyEclipcs中新建Web Project,并导入jar包(我导入了过多的jar包,是为了以后扩展其他框架(Spring)准备)。

184133672.jpg

三、修改web.xml,加入SpringMVC控制中心的servlet配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springMVC/servlet*.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>
                                                                                                                                                                                               
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

四、新建controller。我这里定义了两一个资源路径,但是区别了请求方式,如下代码,当请求为get方式时,页面跳转到login视图,我稍后会将login视图映射到login.jsp;当请求为post时,执行登录操作,我这里假设是完成了登录的业务逻辑,跳转到index视图(index.jsp)。

@Controller
@RequestMapping("Admin")
public class AdminController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("login");
    }
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin() {
        return "index";
    }
}

五、新建controller配置文件。在WEB-INF下建立一个新文件夹“SpringMVC”,并建立一个xml文件,命名为servlet.xml。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 组件扫描 -->
    <context:component-scan base-package="com.lion.controller"/>
                                                                            
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

六、在WEB-INF下建立新文件夹jsp(jsp隐藏),并新建两个jsp文件“index.jsp”,“login.jsp”。

login.jsp中建立一个表单,用于提交post请求。

七、发布程序,启动tomcat。GET请求路径:"http://localhost:8080/MyBlog/Amdin/login.do",则跳转到login.jsp页面,填写好form后提交(POST)到"http://localhost:8080/MyBlog/Amdin/login.do",则进入index.jsp。

八、至此一个简单的基于注解的SpringMVC就搭建好了。