基于Java配置的springMvc处理请求

来源:互联网 发布:手机搭建php服务器 编辑:程序博客网 时间:2024/06/05 07:56

首先看项目目录:



创建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;@Componentpublic class UserImpl implements UserInte {@Overridepublic 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 {@Autowiredprivate 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就会报错。

















原创粉丝点击