基于Java配置的springMvc处理请求

首先看项目目录:



创建user实体:

package spittr;

import java.util.Date;

public class User {

	private String name;
	private String password;
	private Date time;

	public User(){}
	
	public User(String name, String password, Date time){
		this.name = name;
		this.password = password;
		this.time = time;
	}
	

spittr.inte包下存储的是接口文件

package spittr.inte;

import java.util.List;

import spittr.User;

public interface UserInte {

	List<User> findUsers(int count);
}

具体的实现在spittr.impl包下,为了能够让Spring自动声明为一个bean要在 实现类上加@Component注解

package spittr.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import spittr.User;
import spittr.inte.UserInte;

@Component
public class UserImpl implements UserInte {

	@Override
	public List<User> findUsers(int count) {
		List<User> list = new ArrayList<User>();
		for(int i=0; i<count; i++){
			User user = new User("user_"+i, i+"", new Date());
			list.add(user);
		}
		return list;
	}

}

写UserController类,

SpringMvc允许以多种方式将客户端中的数据传送到控制器的处理方法中,包括:

  • 查询参数
  • 表单参数
  • 路径变量

package spittr.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
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.bind.annotation.RequestParam;

import spittr.User;
import spittr.inte.UserInte;

@Component
@RequestMapping("/users")
public class UserController {

	@Autowired
	private UserInte userInte;
	
	@RequestMapping(method=RequestMethod.GET)
	public String users(Model model){
		model.addAttribute("userList", userInte.findUsers(20));
		return "users";
	}
	
	/**
	 * 1、接收查询参数
	 * @param count
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/param", method=RequestMethod.GET)
	public String userList(@RequestParam(value="count", defaultValue="20") int count, Model model){
		model.addAttribute("userList", userInte.findUsers(count));
		return "users";
	}
	
	/**
	 * 2、接收路径变量参数
	 * 如果方法的参数名与占位符的名称相同,可以去掉@PathVariable中的value属性
	 * 即 public String userList2(@PathVariable int count, Model model){
	 * @param count
	 * @param model
	 * @return 
	 */
	@RequestMapping(value="/{count}", method=RequestMethod.GET)
	public String userList2(@PathVariable("count") int count, Model model){
		model.addAttribute("userList", userInte.findUsers(count));
		return "users";
	}
	
	
	@RequestMapping(value="/register", method=RequestMethod.GET)
	public String register(){
		return "register";
	}
	
	
	/**
	 * 3、接收表单参数
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/register", method=RequestMethod.POST)
	public String regist(User user){
		System.out.println(user);
		System.out.println("用户注册的逻辑--------");
		return "redirect:/users";
	}
}

两个页面:

users.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 屏蔽tomcat 自带的 EL表达式 -->
<%@ page isELIgnored="false"%>
<%
	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>Users</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">

</head>

<body>
	<table cellspacing="10px" cellpadding="10px" align="center"
		style="background-color: silver;">
		<c:forEach items="${userList}" var="user">
			<tr>
				<td><c:out value="${user.name}" /></td>
				<td><c:out value="${user.password}" /></td>
				<td><c:out value="${user.time}" /></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

register.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- 屏蔽tomcat 自带的 EL表达式 -->
<%@ page isELIgnored="false"%>
<%
	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>Users</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">

</head>

<body>
	<form action="" method="post"><!-- 这里没有写action jsp会默认提交到展现页面的url -->
		<table cellspacing="10px" cellpadding="10px" align="center"
			style="background-color: silver;">
			<tr>
				<td><input type="text" name="name" placeholder="name" /></td>
			</tr>
			<tr>
				<td><input type="text" name="password" placeholder="password" /></td>
			</tr>
			<!-- <tr>
				<td><input type="date" name="time" placeholder="time" /></td>
			</tr> -->
			<tr>
				<td><button type="submit">提交</button></td>
			</tr>
		</table>
	</form>
</body>
</html>

如果在编译的时候出现No qualifying bean of type 'xxxx' available 则是没有把实体注入进去,修改RootConfig和WebConfig类的@ComponentScan("spittr")

如果在提交表单的时候出现The request sent by the client was syntactically incorrect,那么修改前端和后端接收的字段类型要一致,不一致spring就会报错。

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值