springMVC前后台數據交互


  假設項目需求是在springMVC框架下,后台要傳送一個list到前台,那我們就要做以下幾個步驟:

1 在web.xml文件中進行springMVC的配置:

<?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">
    <display-name></display-name>

    <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.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>

上圖中標紅的區域就是配置前后台交互的重要配置信息,即servlet的配置。

2 在src的目錄下建立一個mvc.xml文件,對應web.xml文件中的<param-value>classpath:mvc.xml</param-value>配置。即渲染器配置,能夠讓前台命令提交的命令精確傳送到后台,后台的數據准確傳送到前台。配置如下:

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- 配置視圖渲染器 -->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- 將視圖名 渲染后視圖的前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 渲染后視圖的后綴 -->
        <property name="suffix" value=".jsp" />
        <!-- 例:視圖名為:hello 渲染后:/WEB-INF/jsp/hello.jsp 該頁面 -->
    </bean>
    <context:component-scan base-package="com.test.controller"/>
</beans>

3 最后是controller,功能相當於ssh框架中的action,功能是處理前台傳過來的命令。從上邊的mvc.xml文件的配置就能看出來,這里我們用的是注解開發,如下:

@Controller
public class ProgramLogController {
    @RequestMapping("/proLog")
    public ModelAndView GetproLog(HttpServletRequest req, HttpServletResponse resp){
        ModelAndView mv = new ModelAndView();
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ProgramLogDao programLogDao = (ProgramLogDao)context.getBean("programLogDao");
        List<ProgramLog> list = programLogDao.selectProgramLog(Constant.REFUSE);
        mv.addObject("msg", list);
        mv.setViewName("index");
        return mv;
    }
}

假設我們傳送過來的是list,那么通過上邊的代碼,打開tomcat服務器,通過在瀏覽器訪問localhost:8080/項目名/proLog.do就可以執行功能了。下面分別附上jsp文件和所引用jar包(jar包多於所需要的)。

jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>programLog</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <table width="80%" align="center">
        <tr>
            <td>序號</td>
            <td>單機序號</td>
            <td>機器號</td>
            <td>動作序號</td>
        </tr>
        <c:forEach items="${msg }" var="log">
        <tr>
            <td>${log.id }</td>
            <td>${log.sid }</td>
            <td>${log.machine_id }</td>
            <td>${log.action_id }</td>
        </tr>
        </c:forEach>
    </table>
  </body>
</html>

jar包

aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.20-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM