码迷,mamicode.com
首页 > Web开发 > 详细

页面回显与URL模板映射

时间:2020-02-15 18:43:43      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:tle   lib   glib   intern   lis   resolve   apt   port   resource   

一.页面回显

对于需要返回界面的数据,可以将后台封装好的数据回显至原始jsp界面中。

举个例子:

User.java

package com.zk.data;

public class User {
	public String sname;
	public String sno;
	public String sid;
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
}

UserCustom.java

package com.zk.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserCustom {
	private User user;

	private List<User> userList;
	
	private Map<String,Object> usermap=new HashMap<String,Object>();
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Map<String, Object> getUsermap() {
		return usermap;
	}

	public void setUsermap(Map<String, Object> usermap) {
		this.usermap = usermap;
	}

	@Override
	public String toString() {
		return "UserCustom [user=" + user + "]";
	}
	
	
}

 SpringMVC.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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
         <context:component-scan base-package="com.zk.Controller"></context:component-scan>
		
		<!-- 配置注解处理器映射器 
			功能:寻找执行类Controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		
		<!-- 配置注解处理器适配器 
			功能:调用controller方法,执行controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
        
         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"></property>
         <property name="suffix" value=".jsp"></property>
         </bean>
         
</beans>

UserController.java

package com.zk.Controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zk.data.User;
import com.zk.data.UserCustom;


@Controller//<bean id="UserController" class="UserController路径">
public class UserController {
	//接受javaBean参数
	@RequestMapping("list")
	public String list(Model model){
		List<User> userList=new ArrayList<User>();
		User user1=new User();
		user1.setSid("1");
		user1.setSname("a");
		user1.setSno("No1");
		
		User user2=new User();
		user2.setSid("2");
		user2.setSname("b");
		user2.setSno("No2");
		
		User user3=new User();
		user3.setSid("2");
		user3.setSname("b");
		user3.setSno("No3");
		
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		
		model.addAttribute("userList", userList);
		
		return "list";
	}
}

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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>回显List集合</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>
    <h1>欢迎来SpringMVC</h1>
    <table border=‘1‘>
    <tr>
    <td>姓名</td>
    <td>ID</td>
    <td>编号</td>
    </tr>
    
    <c:forEach items="${userList}" var="user">
    <tr>
    <td>${user.sname}</td>
    <td>${user.sid}</td>
    <td>${user.sno}</td>
    <td><a href="${pageContext.request.contextPath }/${user.sid}.do">修改</a></td>
    </tr>
    </c:forEach>
    </table>
  </body>
</html>

  运行后结果如下:

技术图片

 

二.URL模板映射

在上面的例子中,list.jsp页面里有行URL模板映射

 <td><a href="${pageContext.request.contextPath }/${user.sid}.do">修改</a></td>

 在后台UserController.java的处理逻辑如下:

package com.zk.Controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zk.data.User;
import com.zk.data.UserCustom;


@Controller//<bean id="UserController" class="UserController路径">
public class UserController {
	//接受javaBean参数
	@RequestMapping("list")
	public String list(Model model){
		List<User> userList=new ArrayList<User>();
		User user1=new User();
		user1.setSid("1");
		user1.setSname("a");
		user1.setSno("No1");
		
		User user2=new User();
		user2.setSid("2");
		user2.setSname("b");
		user2.setSno("No2");
		
		User user3=new User();
		user3.setSid("2");
		user3.setSname("b");
		user3.setSno("No3");
		
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		
		model.addAttribute("userList", userList);
		
		return "list";
	}
	@RequestMapping("/{id}")
	public String update(@PathVariable Integer id){
		System.out.println(id);
		
		return "redirect:list.do";
	}
}

  它将返回list.jsp界面,并输出id值。

技术图片

 

页面回显与URL模板映射

标签:tle   lib   glib   intern   lis   resolve   apt   port   resource   

原文地址:https://www.cnblogs.com/longlyseul/p/12312871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!