Spring MVC类型转换的相关说明

Java-框架王 SpringMVC 发布时间:2021-05-14 22:59:30 阅读数:14646 1
下文讲采用示例的方式,讲述相关类型说明,如下所示:

insert.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>添加信息</title>
</head>
<body>
<form action="insert" method="post">
<table border=1 bgcolor="lightblue" align="center">
<tr>
<td>名称:</td>
<td><input class="textSize" type="text" name="name" /></td>
</tr>
<tr>
<td>信息</td>
<td><input class="textSize" type="text" name="info" /></td>
</tr>
<tr>
<td>数量</td>
<td><input class="textSize" type="text" name="qty" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交" />
</td>
</tr>
</tab1e>
</form>
</body>
</html>


页面的数据将提交到方法提交到 insert 的 Servlet中,
该 Servlet 将这些请求信息封装成一个info类的值对象。

info 类的代码如下:

package com.java265;

public class info {
private String name;
private double qty;
private String info;

// 无参数的构造方法
public  info() {
}

// 有参数的构造方法
public info(String name, string info, double qty) {
super();
this.name = name;
this.qty = qty;
this.info = info;
}
 

// 此处省略了setter和getter方法
}

insertServlet 类的代码如下:

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java265.info;

public class insertServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
// 设置编码,防止乱码
request.setCharacterEncoding("utf-8");
// 获取参数值
String name= request.getParameter("name");
String info= request.getParameter("info");
String qty = request.getParameter("qty");
// 下面进行类型转换
double qtyExtend= Double.parseDouble(qty); 
// 将转换后的数据封装成goods值对象
info i = new info(name, info, qtyExtend);
// 将info值对象传递给数据访问层代码省略
...
}
}


 



上文讲述Servlet开发中进行类型转换,并封装为对象 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringMVC/2021-05-14/436.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者