【SpringMVC】基於註解的SpringMVC簡單示例

一、SpringMVC下載

在Spring官網下載Spring Framework

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

二、在MyEclipcs中新建Web Project,並導入jar包(我導入了過多的jar包,是爲了以後擴展其他框架(Spring)準備)。

三、修改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就搭建好了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章