zoukankan      html  css  js  c++  java
  • springMVC3学习(六)--SimpleFormController

    SimpleFormController提交表单流程例如以下:

    login.jsp

    <form action="login" method="post">
    	用户名:<input type="text" name="username"/></br>
    	密码:<input type="password" name="password"/></br>
    	<input type="submit" value="提交">
    </form>

    springMVC.xml

    <!-- 处理器 -->
    <bean name="/login" class="com.itmyhome.Login">
    	<!-- 返回处理成功页面  success.jsp -->
    	<property name="successView" value="success"/>
    </bean>
    
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    
    <!-- 定义跳转的文件的前后缀 -->
    <bean id="viewResolver"
    	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/" />  <!-- 默认放在WebRoot下 -->
    	<property name="suffix" value=".jsp" /> 
    </bean>

    UserModel.java

    public class UserModel {
    	private String username;
    	private String password;
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }

    Login.java

    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    public class Login extends SimpleFormController {
    	public Login() {
    		setCommandClass(UserModel.class);// 设置命令对象实现类
    		setCommandName("user");// 设置命令对象的名字
    	}
    	//下面内容省略
    	/*protected Object formBackingObject(HttpServletRequest request) throws Exception { 
    		return null;
    	} 
    	protected Map referenceData(HttpServletRequest request) throws Exception { 
    		return null;
    	}*/
    	public void doSubmitAction(Object obj) throws Exception {
    		UserModel user = (UserModel) obj;
    		System.out.println(user);
    	}
    }

    success.jsp

    <body>
      注冊成功,欢迎: ${user.username }!
    </body>

    測试:

    浏览器中输入:http://localhost:8080/spring_SimpleFormController/login.jsp 进入登录页面

    注冊成功页面:

    源代码下载:http://download.csdn.net/detail/itmyhome/7366879


    欢迎添�JAVA技术交流群:74955800


  • 相关阅读:
    unity free asset
    Unity3d Serialize问题
    野蛮能带来繁荣是怎么回事?
    如何给unity3d工程加入依赖的android工程
    unity3d 导入google play services插件工程
    NGUI中UILabel使用url标签的一个bug
    数据结构
    git命令
    面试算法经典问题
    Http Client 源码分析
  • 原文地址:https://www.cnblogs.com/yxwkf/p/3900662.html
Copyright © 2011-2022 走看看