码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-06-18 16:19:25      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:method   mic   表单   asi   doctype   sim   ring   targe   character   

           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

技术分享

技术分享

项目结构图

技术分享


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

标签:method   mic   表单   asi   doctype   sim   ring   targe   character   

原文地址:http://www.cnblogs.com/cynchanpin/p/7044486.html

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