SpringMVC環境配置

所需要的包可在本站下載http://download.csdn.net/detail/fonxian/9079125

在WEB-INF下配置web.xml和spring-servlet.xml(若web.xml中servlet-name
設置爲xxx,則配置文件也要改成xxx-servlet.xml,這裏需要特別注意),只需要配置這兩個文件就好。
1、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 核心控制器 -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

2、spring-servlet.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context"
       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"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        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/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">

    <!-- 開始spring mvc的註解 -->  
    <mvc:annotation-driven />
    <!-- 配置註解掃描的包路徑 -->  
    <context:component-scan base-package="com" />
     <!-- 配置action中返回的視圖配置  -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

3、在src文件夾下新建一個controller

@Controller
public class MeetingController {

    @RequestMapping(value="/getuser")
    public String getName(String userName,Model model){
        model.addAttribute("username", userName);
        return "index";
    }
}

這裏的意思是當服務器接收到一個getuser的請求,這個請求會通過dispatch servlet轉發到Controller中,Controller對數據進行操作(這裏的model.add..是把參數帶到要跳轉的頁面),然後跳轉到index.jsp中。
index的前綴和後綴就在spring-servlet.xml中配置的

<property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />

4、然後需要寫一個發送請求的界面和一個跳轉的界面
發送請求的界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="getuser"  method="post">
        <input type="text" name="name" />
        <input type="submit" value="tijiao"/>
    </form>
</body>
</html>

接收請求的界面index.jsp,這裏的username是與model.addAttribute(“username”, userName)對應起來的

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>${username }</p>
</body>
</html>

5、如果需要返回的是一個列表,例如

ArrayList<User> userlist = ......;
model.addAttribute("userlist", userlist);

則界面可以這樣寫

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:forEach items="${userlist}"  var="p">
        <tr>
        <td>${p.id}</td>
        <td>${p.name}</td>
        </tr>
    </c:forEach>
</body>
</html>

6、如果出現錯誤

Null ModelAndView returned to DispatcherServlet

需要檢查一下@RequestMapping(value=”/getuser”)下面有沒有加 @ResponseBody,加了這個註解,則返回的是Json串,不能再使用return “index” 這種方式

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