• 第二十四天 框架之痛-Spring MVC(四)


               6月3日,晴。“绿树浓阴夏日长。 楼台倒影入池塘。 水晶帘动微风起, 满架蔷薇一院香”。

          以用户注冊过程为例。我们可能会选择继承AbstractController来实现表单的显示。

          1、导入spring-framework-2.5.6的jar包,详第八天 框架之痛-Spring MVC(一)

          2、编写web.xml 的配置文件。主要增加spring Web MVC框架提供的

               org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题。

    <?

    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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>myOneMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myOneMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

            3、myOneMVC-servlet.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:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    	
    	<bean name="/reg" class="edu.eurasia.controller.RegControl"></bean>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/WEB-INF/jsp/"></property>
             <property name="suffix" value=".jsp"></property>
        </bean>     
    </beans>
             4、后端控制器RegControl.java

    package edu.eurasia.controller;
    
    import org.springframework.validation.BindException;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    public class RegControl extends SimpleFormController {
    
    	public RegControl() {
    		setCommandClass(User.class);
    	}
    
    	@Override
    	protected ModelAndView onSubmit(Object object, BindException errors)
    			throws Exception {
    		User user=(User)object;
    		ModelAndView mav = new ModelAndView("hello");
    		mav.addObject("user", user);
    		return mav;
    		
    	}
    }
    
              RegControl.java继承SimpleFormController类,构造器中调用setCommandClass方法绑定定命令对象(这里为User类),转换object为User类进行业务逻辑操作。onSubmit()方法能够返回一个ModelAndView对象,完毕向页面输出数据的功能。

             5、表单相应的POJO-User.java

    package edu.eurasia.controller;
    
    public class User {
    	private String username;
    	private int age;
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    }
    
             6、表单填写页面index.jsp

    <%@ 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>
    	<form action="reg" method="post">
    		用户名:<input type="text" name="username"><br/>
    		年龄:<input type="text" name="age"><br/>
    		<input type="submit">
    	</form>
    </body>
    </html>
                     7、返回一个对应的给client页面hello.jsp
    <%@ 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>
    	Brazil世界杯,你好!<br/>
              用户名:${user.username }<br/>
             年龄:${user.age }
    </body>
    </html>

                   8、执行Tomcat 7 。输入http://localhost:8080/springMVC_controllerweb_1/index.jsp

    项目结构图



  • 相关阅读:
    AOP的相关理论介绍
    代码访问数据库时没有报错一直转圈,MySQL不同驱动版本的连接方式
    Linux(CentOS7)设置固定获取IP地址
    NodeJS的安装与配置
    IDEA启动tomcat访问localhost:8080出现404错误
    Win10笔记本电脑外接显示器,如何在笔记本合上的时候让他继续在显示器上使用
    SpringBoot集成Swagger-Bootstrap-UI(已改名为Knife4j)
    Redis学习之Jedis的操作和使用
    SpringBoot整合Swagger2,搭建Restful API在线文档
    IDEA创建SpringBoot项目时出现:Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings.
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7044486.html
Copyright © 2020-2023  润新知