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

优质内容筛选与推荐>>
1、memstr - Dustfly的专栏 - 博客频道 - CSDN.NET
2、scala -- 继承
3、软件工程2第一次作业
4、第51条:精简initialize与load的实现代码
5、POJ 1833


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn