初學SpringMVC的框架簡單搭建以及工作原理實例

SpringMVC的工作原理與struts2的工作原理差不多,但比struts2更簡潔點

1. 導jar包

2. 創建自己起名的spring-servlet.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: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/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- SpringMVC的註解已經注入 具體到掃描包-->
    <context:component-scan base-package="controller"/>

    <!-- SpringMVC中的controller中return的值傳遞給本xml文件,通過該下方式,拼接成相應jsp路徑
    例如,return 個welcome,直接跳轉到/WEB-INF/jsp/welcome.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3. web.xml編寫配置 (servlet-name中是自己起名的那個名稱)

<servlet>
        <servlet-name>renjia</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>renjia</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

4.編寫jsp與Controller

index.jsp

<!-- jsp中表單提交傳遞action值到controller中,去找相應的註解,去調用對應的方法 -->
      <form action="haha" method="post">
        action(${action})<br>
        姓名:<input type="text" name="name"/><br/>
        年齡:<input type="text" name="age"/><br/>
        參數:<input type="text" name="str1"/><br/>
        <input type="submit" value="提交"/>   
    </form>

welcome.jsp

<body>
 <!--  EL表達式接受action傳來的參數 -->
    welcome!!!${student.name}--->${student.age}---->${str1}
</body>

MyController.class

package controller; 

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import bean.Student;
@Controller
public class MyController{

    //1將index頁表格信息傳入welcome頁,用Map向頁面傳參數
    @RequestMapping("/haha")
    public String hello(Student student,String str1,Map<String,Object> context){
        System.out.println(student.getName());
        System.out.println(student.getAge());
        System.out.println(str1);

        context.put("student",student);
        context.put("str1",str1);

        return "welcome";
    }

    //2接受index頁面傳過來的三個參數  跟index中的name一樣
    @RequestMapping("/hehe")
    public String nihao(HttpServletRequest request,Model model){
        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("age"));
        System.out.println(request.getParameter("str1"));
        //將student對象和頁面傳過來的str1放在model裏作爲一個整體的模型
        Student student1 = new Student();
        student1.setName(request.getParameter("name"));
        student1.setAge(Integer.valueOf(request.getParameter("age")));
        model.addAttribute("student",student1);
        model.addAttribute("str1", request.getParameter("str1"));
        return "welcome";   
    }

    //3用字段的註解的形式創建字段對象,接收index頁面傳來的字段信息
    @RequestMapping("/heihei")
    public String hello1(@RequestParam(value="name") String name,
            @RequestParam(value="age") int age,
            @RequestParam(value="str1") String str1,
            Map<String,Object> context){
        //想讓傳遞的參數顯示在目標的話,傳遞方式(例Map)需要在傳遞前就要創建好,否則就會報500空指針
        //不能在函數裏新創個Map對象來put值再顯示在頁面
        System.out.println(name);
        System.out.println(age);
        System.out.println(str1);
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        context.put("student", student);
        context.put("str1", str1);
        return "welcome";
    }

    //直接用模型註解,將傳來的東西給塑造成一個模型給傳過來
    @RequestMapping("/hengheng")
    public String hello2(@ModelAttribute Student student,String str1,
            Map<String,Object> context){
        System.out.println(student.getAge());
        System.out.println(student.getName());
        System.out.println(str1);
        context.put("student", student);
        context.put("str1", str1);
        return "welcome";

    }
}
發佈了25 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章