Spring MVC技术之【上机】
1.【上机】配置mvc环境
2.【上机】使用注解的方式实现Spring MVC的参数的传递(页面上传入和传出)
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 1.配置注解 --> <context:annotation-config></context:annotation-config> <context:component-scan base-package="renjiale"></context:component-scan> <!-- 2.MVC的注解配动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3.配置viewresovler视图处理器 --> <!-- rest风格: http://域名/index.jsp --> <bean id="myspringmvc" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp" ></property> <!-- 根据前缀和后缀找到对应的页面,OK --> </bean> <!-- 4. 编写代码--> <!-- 5.配置dispatcherServlet(MVC)自动配置;在web.xml配置servlet --> <!-- 6.编写servlet --> <bean id="dispatcherServlet" class="org.springframework.web.servlet.DispatcherServlet"></bean> <!-- 7.编写测试代码-jsp页面 --> <!-- 8.MVC处理请求,编写业务类 --> <!-- 9. 当在.jsp点击请求,让业务类中的方法来处理请求 --> <!-- 10.controller使用注解实现控制器 --> <!-- 11.jsp请求找多个业务类 @RequestMapping 找对应的方法@RequestMapping--> <!-- 12.请求后.jsp页面跳转到指定地方 --> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>web_cookie</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>mvc01</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mvc01</servlet-name> <!-- 注: 所有请求MVC来处理 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>这里是index.jsp页面</h1> <a href="mav/show">测试</a> <!-- MVC处理请求 --> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>这里是sucess页面</h1> </body> </html>
package renjiale.controller; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/mvc") public class MyFormMVC { @RequestMapping(value="show") public ModelAndView show() { System.out.println("show方法执行了"); ModelAndView mav = new ModelAndView(); mav.setViewName("suc"); return mav; } /*@RequestMapping(value="way2") public String show2(@RequestParam(value="msg")String myMsg,@RequestParam(value="str")String mystr) { System.out.println("show2()方法执行了............"); System.out.println("从页面上接收到的信息:"+"msg:"+myMsg+",mystr:"+mystr); return "suc"; }*/ /*@RequestMapping(value="show2") public String show2(String msg,String str) { System.out.println("从页面上接收到的信息:"+"msg:"+msg+",mystr:"+str); return "suc"; }*/ @RequestMapping(value="show2") public String show2(HttpServletRequest request,Model model) { String msg = request.getParameter("msg"); String str = request.getParameter("str"); System.out.println("从页面上接收到的信息:"+"msg:"+msg+",mystr:"+str); //处理完请求, 如何把参数传回到页面 //数据返回到页面 String reply =str +"myspringmvc"; //把数据信息传到 suc.jsp页面上 request.setAttribute("reply", reply); //MVC更简洁的方式传递参数 model.addAttribute("str",reply); return "suc"; } }
3.【上机】实现Spring MVC注册实现和过程中的一些问题
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>是注册页面</h1> <!-- get方式是地址栏信息提交 --> <!-- http://localhost:8080/Spirng_mvc_form/mvc/reg?username=renjiale&pwd=123456&confirmpwd=123456&realname=%E4%BB%BB%E5%AE%B6%E4%B9%90 --> <form action="mvc/reg" method="get"> 用户名: <input type="text" name="username" value="renjiale"><br/> 密码:<input type="password" name="pwd" value="123456"><br/> 确认密码:<input type="text" name="confirmpwd" value="123456"><br/> 真实姓名:<input type="text" name="realname" value="任家乐"><br/> <input type="submit" value="注 册"> </form> </body> </html>
package renjiale.controller; import java.io.UnsupportedEncodingException; /** * 1. 页面上 post请求-get (HTTP Status 405 – Method Not Allowed) * 2. post - method=RequestMethod.POST 可以 * 3. get - method=RequestMethod.GET 可以 乱码: 1.filter过滤器 2.get方式 a.new String b.connector-port * */ import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import renjiale.pojo.Users; @Controller @RequestMapping("/mvc") public class MyFormMVC { @RequestMapping(value="reg", method=RequestMethod.GET) public String registerMethod(Users users,HttpServletRequest request) { System.out.println("注册方法执行了"); String realname = users.getRealname(); try { realname = new String(realname.getBytes("IOS-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(users.getUsername()+users.getPwd()+users.getConfirmpwd()+users.getRealname()); return "suc"; } }
package renjiale.pojo; /*
*通过实体类, 封装数据
*/ import java.io.Serializable; public class Users implements Serializable { private String username; private Integer pwd; private Integer confirmpwd; private String realname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getPwd() { return pwd; } public void setPwd(Integer pwd) { this.pwd = pwd; } public Integer getConfirmpwd() { return confirmpwd; } public void setConfirmpwd(Integer confirmpwd) { this.confirmpwd = confirmpwd; } public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname; } @Override public String toString() { return "Users [username=" + username + ", pwd=" + pwd + ", confirmpwd=" + confirmpwd + ", realname=" + realname + "]"; } }
4.【上机】实现Spring MVC和AJAX的交互,包括接收json请求参数
5.【上机】使用Spring MVC实现文件上传
6.【上机】Spring MVC使用二种方式实现自定义拦截器
7.【上机】使用自定义拦截器实现用户登录验证
8.【上机】集成Spring与Spring MVC
9.【上机】集成Spring与Spring MVC加JDBC,实现对用户的CRUD操作
show your code,talk is cheap