springmvc+jsp/html 前后台交互(二):controller和jsp页面交互

来源:互联网 发布:数据恢复软件二点零 编辑:程序博客网 时间:2024/06/05 16:22

Controller使用的注释的方法进行访问,以下是Demo:

@Controller@RequestMapping("/user")public class UserController {@Resourceprivate IUserService userService;@RequestMapping("/list")public String toIndex(HttpServletRequest request,Model model){String openId = request.getParameter("openid");User user = this.userService.getUserByOpenId(openId);model.addAttribute("user", user);return "userList";}}


上面得到userList就是位于配置文件(spring-mvc.xml)中的jsp视图解析器的文件路径:/WEB-INF/view/jsp/



展示层jsp代码如下(这个jsp页面是从controller取的数据展示):

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><meta name="viewport"    content="width=device-width,minimum-scale=1.0, maximum-scale=2.0" />  <head>    <title>测试</title>    <style type="text/css">ul {    list-style:none;    margin-top: 15%;    font-size: smaller    }li{    line-height: 30px;    color: balck    }.title{    margin-top: 10%;    color: #48a94c;    font-weight: bold;    font-size: smaller     } .hr0{     width: 100%;    float: left } .userInfo{     margin-left: 90px }</style>   </head>    <body>  <div class="title">您的注册信息</div>  <hr class="hr0"/>  <ul>  <li>联系电话<span class="userInfo">${user.telNumber}</span></li>  <li>号码1<span class="userInfo">${user.openNumber}</span></li>  <li>号码2<span class="userInfo">${user.id}</span></li>  <li>号码3<span class="userInfo">${user.openId}</span></li></ul><div class="title">测试</div>  <hr class="hr0"/>  </body></html>


注册页面(form表达向后台提交数据)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ include file="/WEB-INF/view/include/taglib.jsp" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><meta name="viewport"content="width=device-width,minimum-scale=1.0, maximum-scale=2.0" /><head>    <base href="<%=basePath%>">        <title>注册</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 href="<%=request.getContextPath()%>/css/register.css" type="text/css" rel="stylesheet">        <link href="../css/global.css" type="text/css" rel="stylesheet">        <script type="text/javascript" src="js/register.js"></script>  </head>    <body>  <div id="layout">            <span>注册即启用!<%=basePath%></span>          <form:form modelAttribute="user" method="post" action="${pageContext.request.contextPath}/user/save">          <table>          <tr>                <td> 号码:</td>                <td><form:input path="plateNumber" /></td>                          </tr>           <tr>                <td> 联系电话:</td>                <td><form:input path="telNumber" type="tel" /></td>                          </tr>                    </table>          <input id="btnSubmit"  type="submit" value="提 交"/>          </form:form>             </div>      </body></html>


注意:jsp页面form表单提交的action路径需要项目上下文路径,使用

${pageContext.request.contextPath}即可,这样form表单就会以User对象的形式传到后台controller /user/save方法中

原创粉丝点击