1.包结构

2.我们需要对web.xml进行配置:

<?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>jFinal_formdate</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>cn.siggy.config.BaseConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

<init-param>标签中的value值根据自己创建的包名和类名而定。

3.在src包下创建cn.siggy.config包,在该包下创建BaseConfig类:

package cn.siggy.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.render.ViewType; import cn.siggy.controller.UserController; public class BaseConfig extends JFinalConfig{ @Override
public void configConstant(Constants cnst) {
//开发模式:可以自动启动
cnst.setDevMode(true);
//指定视图
cnst.setViewType(ViewType.JSP);
} @Override
public void configHandler(Handlers arg0) {
// TODO Auto-generated method stub } @Override
public void configInterceptor(Interceptors arg0) {
// TODO Auto-generated method stub } @Override
public void configPlugin(Plugins arg0) {
// TODO Auto-generated method stub } @Override
public void configRoute(Routes route) {
//1.通过路由进行映射,通过route与方法名进行映射,这里只用写/user不用写/add,因为add为映射的方法名。
route.add("/user",UserController.class); } }
通过路由进行映射,通过route与方法名进行映射,这里只用写/user不用写/add,因为add为映射的方法名。

4.创建user_add.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="user/add" method="post">
用户名:<input type="text" name="name"/><br>
年龄:<input type="text" name="age"/><br/>
<input type="submit" value="添加"/> </form>
</body>
</html>

form表单提交到user/add中进行处理,我们需要在BaseConfig中配置映射:在configRoute中配置路由,提交之后进入到BaseConfig中寻找路由,首先寻找到user对应的类(UserController),然后进入到该类中add函数进行处理。

5.创建UserController,获取表单数据:

package cn.siggy.controller;

import com.jfinal.core.Controller;
import com.jfinal.render.ViewType; import cn.siggy.vo.User; public class UserController extends Controller{ public void add () {
//2.获取表单的值:通过getPara来获取表单值
String name=getPara("name");
int age=getParaToInt("age");
System.out.println("姓名:"+name+"\t年龄:"+age);
//3.如何设置回变量值:通过setAttr。
setAttr("msg", "添加成功!!!");
//4.如何跳转页面进行渲染:通过render来指定跳转的页面.
//首先我们应该在Config中的configConstant指定视图:cnst.setViewType(ViewType.JSP);
render("/success.jsp");
} }

获取到表单数据后,将“添加成功”信息返回给success.jsp页面。

6.创建success.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>
${msg }
</body>
</html>

这里通过el表达式获取参数的值.

7.运行:

启动项目,在浏览器中输入:http://localhost/user_add.jsp,进入到jsp页面,在这个页面中输入姓名和年龄:

点击提交,进入到success.jsp页面,显示:

8.我们总结一下MVC的四个步骤

(1)通过路由进行映射,通过route与方法名进行映射。也就是BaseConfig类中的这个方法;

@Override
public void configRoute(Routes route) {
//1.通过路由进行映射,通过route与方法名进行映射,这里只用写/user不用写/add,因为add为映射的方法名。
route.add("/user",UserController.class); }

(2)获取表单的值:通过getPara来获取表单值。

String name=getPara("name");
int age=getParaToInt("age");

(3)如何设置回变量值:通过setAttr。

setAttr("msg", "添加成功!!!");

(4)如何跳转页面进行渲染:通过render来指定跳转的页面.首先我们应该在Config中的configConstant指定视图:cnst.setViewType(ViewType.JSP);

render("/success.jsp");

使用JFinal实现使用MVC获取表单中的数据并将提示信息返回给另一jsp页面。的更多相关文章

  1. jquery获取表单中的数据

               <form>                 <input name="username" type="text"/&g ...

  2. 在Action中获取表单提交数据

    -----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...

  3. 8.javascript获取表单中两个数字,并判断大小

    获取表单数据的方式: 1.表单注意些id 2.使用document.getElementById("num").value 获取值 3.一定要注意现在得到是string类型,可以用 ...

  4. PHP 输入输出流 php://input 获取表单中2个重名name的值

    PHP 输入输出流 php://input   获取表单中2个重名name的值 <?php // PHP有一种"所有IO都是流"的说法. // 压缩流参考 https://w ...

  5. 今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式

    今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式 jquery中的serialize() 方法 该方法通过序列化表单值,创建 URL 编码文本字符串 序列化的值可在 ...

  6. DHTMLX 前端框架 建立你的一个应用程序 教程(十)--保存表单中的数据

    保存表单中的数据 现在我们所要做的是 当用户点击提交按钮的时候  我们将表单中的数据进行保存操作. 我们可以使用dhtmlxDataProcessor. 来进行操作.它是一个数据组件,可以提供与服务器 ...

  7. Struts2_day02--Action获取表单提交数据

    Action获取表单提交数据 1 之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap 2 ...

  8. ASP.NET MVC 获取表单数据

    public class Person { public string Name{get;set;} public string Phone{get;set;} } view层 @model Mode ...

  9. 在jsp中用EL 表达来获取表单中的参数

     在一个JSP页面转到另一个JSP页面时,对表单中的参数用EL表达式提取为:     <form action="sampleJsp.jsp" method="po ...

随机推荐

  1. mysql 忘记密码解决方案

    Mysql 忘记root密码的完美解决方法 转载  2016-12-23   作者:MR.QiGao    我要评论 通常在使用Mysql数据库时,如果长时间没有登陆,或者由于工作交接完成度不高,会导 ...

  2. Linux性能测试分析命令_top

    top命令动态展示系统整体资源和各个进程资源占用状况,是Linux下常用的性能分析工具. top命令语法 使用格式:top [-] [d] [b] [H] [p] [q] [c] [C] [S] [s ...

  3. C++随记

    1.const限定符 const限定变量的值不可变,并且const对象必须要初始化 const int buf = 512; //正确,表明buf的值为512 buf = 400;  //错误,buf ...

  4. oracle理解和导入导出

    搞过sql server的程序员很难理解oracle的表空间.我在这里简单说一下吧, oracle中的表空间就相当于sql server中的实例,用户就相当于sql server中的库. 所以在ora ...

  5. 吴裕雄 25-MySQL 临时表

    MySQL 临时表MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间.临时表在MySQL 3.23版本中添加,如果你 ...

  6. Hadoop集群(一) Zookeeper搭建

    作为Hadoop初学者,自然要从安装入手.而hadoop的优势就是分布式,所以,也一定要安装分布式的系统. 整体安装步骤,包括Zookeeper+HDFS+Hbase,为了文章简洁,我会分三篇blog ...

  7. pycahrm 基础设置

    一些常用设置: 1. pycharm默认是自动保存的,习惯自己按ctrl + s 的可以进行如下设置:1. file -> Setting -> General -> Synchro ...

  8. tensorflow降低版本

    tensorflow降低版本: pip install tensorflow==1.2.0 查看版本: import tensorflow as tf print(tf.__version__)

  9. centos7 防火墙 开启端口 并测试

    1.防火墙 CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用fir ...

  10. 总是Eqw

    1.投递总是Eqw状态 qstat -j job_ID #Eqw状态的job id qconf -sq all.q |grep host qconf -shgrp @allhosts