springmvc02----参数绑定

本文深入探讨SpringMVC中的参数绑定技术,包括基本类型绑定、POJO绑定、包装POJO绑定及自定义绑定,详细解析如何在Controller层处理前端传递的参数。

springmvc的参数绑定:

当一个页面点击修改会带走一个参数id

即jsp的这个参数id

<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>

而Conrtroller层会有一个修改方法(参数id),通过这个参数id会知道改哪条数据;

而这个参数id只能是形参式的,而不能是成员变量式的。

因为如果是成员变量式的,每条进程来修改都会改变这个参数id,会引起冲突,对其他进程有影响

单例模式下处理多线程:

默认参数绑定:即原始servlet开发

service层:

public interface ItemService {

 public Items selectbyid(Integer id);
}
@Service
public class ItemServiceimpl implements ItemService {
    @Autowired
    private ItemsMapper itemsMapper;

 
    @Override
    public Items selectbyid(Integer id) {
        return itemsMapper.selectByPrimaryKey(id);
    }

controller层

@Controller
public class controllertest {

    @Autowired
    private ItemService itemService;

    @RequestMapping("itemEdit")
    public ModelAndView to(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

         servlet开发
        String id = request.getParameter("id");
        Items items = itemService.selectbyid(Integer.parseInt(id)); 

/*        Items items = itemService.selectbyid(id);*/
        ModelAndView View = new ModelAndView();
        View.addObject("item", items);
        View.setViewName("editItem");

        return View;
    }

简单类型参数绑定:适用于查询(select)

只改controller层:在参数上加上Integer id/string/float/double/boolean,

具体根据id=${item.id},也就是取决pojo类中的类型

参数名Integer id要与id=${item.id}要一致

<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
@Controller
public class controllertest {

    @Autowired
    private ItemService itemService;

    @RequestMapping("itemEdit")
    public ModelAndView to(Integer id,HttpServletRequest request, HttpServletResponse response, HttpSession session) {
 

        Items items = itemService.selectbyid(id);
        ModelAndView View = new ModelAndView();
        View.addObject("item", items);
        View.setViewName("editItem");

        return View;
    }

 参数绑定之pojo:适用于更改数据(update)

因为更改数据不可能只带来一个数据,肯定包含各种各样的数据库表的数据类型

要求:

jsp页面的input标签中name=“属性名”,要与pojo类的属性名要一致

service层;
 

public interface ItemService {
 
    public void update(Items items);
}
@Service
public class ItemServiceimpl implements ItemService {
    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public void update(Items items) {
        items.setCreatetime(new Date());
        itemsMapper.updateByPrimaryKeyWithBLOBs(items);
    }

 

controller层:

@Controller
public class controllertest {

    @Autowired
    private ItemService itemService;

    @RequestMapping("updateitem")
    /**
     *<form id="itemForm"	action="${pageContext.request.contextPath }/updateitem.action" method="post">
     *     经过提交页面,action直接到这个RequestMapping,实行这段代码
     */
    public ModelAndView update(Items items) {

        itemService.update(items);

        ModelAndView View = new ModelAndView();
        /**
         *         将pojo的值存到页面,并取名为item好方便传值
         *         想相当于request.setattribute(),存数据只放到request作用域中
         */
        View.addObject("item", items);
        /**
         * 跳转到某页面去
         */
        View.setViewName("success");
        return View;
/**
 * 问题:
 * 页面跳转成功,但是数据插入不到数据库里去
 * 解决:
 * editItem.jsp中input标签的name="pojo类的属性名要一 一对 应" 而el表法式的${item.id/name/...},则是 View.addObject("item", items);的作用,具体请看el
 */

    }

参数绑定之包装pojo

就是建立一个包装类,把pojo类注入,set/get

然后把参数换成Query vo即可,那么jsp页面input标签中name=“items.name/Date”

参数绑定之自定义绑定

例如日期;我想定义一个

yyyy:MM-dd HH_mm-ss这种类型的日期

但是日期没有这种格式,所以自定义

在springmvc.xml中的处理器适配器中 配置 转换工厂,在转换工厂中配置具体要转换的东西

<mvc:annotation-driven  conversion-service="formattingConversionServiceFactoryBean"/>
    <!--配置转换器的工厂-->
    <bean id="formattingConversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!--配置转换器-->
        <property name="converters">
            <list>
                <!--转换日期类型-->
                <bean class="com.ssm.conversion.DateConveter"/>
            </list>
        </property>
    </bean>

需要创建一个转换日期的自定义的类:

1.实现Conver接口

S:页面传回来的类型:string

T:传出去的类型:日期date

  将页面接收到的string类型格式识别,然后转成日期类型

public class DateConveter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {

        try {
            if (s != null) {
              
                DateFormat dateFormat = new SimpleDateFormat("yyyy:MM-dd HH_mm-ss");
                return dateFormat.parse(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值