Spring MVC一些知識

  1. 首先,我們使用spring mvc需要導入的jar包
    需要導入的jar包信息
  2. 在wen.xml需要配置的信息
    web.xml需要的配置信息

從上往下一次介紹配置文件的信息


  1. controller層(servlet層)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Controller方法調用規則定義 -->
    <beanid="paraMethodResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"/>
        <property name="defaultMethodName" value="list"/>
    </bean>
   <!-- 頁面View層基本信息設定 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 完善具體Controller裏面的返回信息,如:return index -->
        <!-- 增加前綴/myjsp/index -->
        <!--<property name="prefix" value="/myjsp/"/>-->
        <!-- 增加後綴:index.jsp -->
        <property name="suffix" value=".jsp"/>
    </bean>
<!-- servlet映射列表,所有控制層Controller的servlet在這裏定義 -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!-- 如果jsp頁面中有user.do的action,就交給userController處理:相當於一個servlet處理user.do的請求 
                    userController參照web.config.xml裏面配置bean的id名-->
                <prop key="user.do">userController</prop>
            </props>
        </property>
    </bean>

<!-- 配置userController具體的類名信息 -->
<bean id="userController" class="com.sxt.action.UserController">
    <!-- 實現注入:將userService注入到UserController裏面,
    這裏的ref參照service-config.xml裏面的bean的id 
    這裏的name要與userCotroller裏面的屬性名對應-->
    <property name="userService" ref="userService"></property>
</bean>
</beans>
相應userController的寫法

public class UserController implements Controller {

//接受web-config.xml裏面注入的參數
private UserService userService;

//實現Controller裏面的方法
public ModelAndView handleRequest(HttpServletRequest req,
        HttpServletResponse resp) throws Exception {
    System.out.println("HelloController.handleRequest()");
    req.setAttribute("a", "aaaa");
    userService.add(req.getParameter("uname")); 
    //更加web-config.xml裏面的配置信息,可以知道跳轉到WebRoot/index.jsp頁面
    return new ModelAndView("index");
}

public UserService getUserService() {
    return userService;
}

public void setUserService(UserService userService) {
    this.userService = userService;
}

}

  1. service層:
    配置文件信息
    這裏寫圖片描述
    具體類的信息:
    這裏寫圖片描述

  2. dao層
    配置文件信息這裏寫圖片描述
    代碼段信息

最後jsp頁面的信息

 <body>
    <form action=user.do>
        用戶名:<input type=text name=uname /><br/>
        <input type=hidden name=method value="reg"/>
        <input type=submit value= 註冊   />
    </form>     
  </body>

這是用spring mvc實現的一個小功能!能夠觀察整個框架的結構

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