SpringMVC表单回显

做课程设计的时候遇到一个问题弄了好久,找了好久也没有找到解决办法,弄了好久终于弄出来了,人生的第一篇博客来补充一下吧,
先说Eclipse抛出的异常:
严重: Invalid property ‘rigth’ of bean class [com.learn.train.entitis.User]: Bean property ‘rigth’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.NotReadablePropertyException: Invalid property ‘rigth’ of bean class [com.learn.train.entitis.User]: Bean property ‘rigth’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
在不断试验了一晚上之后终于发现了原因。
使用SpringMVC的表单回显,比较容易出错的几个地方:
1:回显修改的界面
暂时只做了回显的部分,对于数据的校验部分还没有写。
使用 modelAttribute把Handller里面有的和他同名的对象 是在请求域中存在的
注意 path的值是Word这个对象里面的属性 比如在Word写了 private String lastname;那么path的值就应该是 lastname;
还有 要在JSP页面上导入 <%@ taglib prefix=”form” uri=”http://www.springframework.org/tags/form” %>

    <form:form action="${pageContext.request.contextPath }/manager" 
                method="POST" modelAttribute="word">
        单词:<form:input path="word"/>
        正确答案:<form:input path="rigth"/>
        干扰选项:<form:input path="interfere"/>
        难度等级:<form:input path="level"/>
        <input type="submit" value="提交修改"/>
    </form:form>

2:Handller 里面:
使用 @ModelAttribute注解声明一个方法 这个方法就会在渲染回显页面之前把数据查询出来填在请求域中,在页面上只需要把path的值写对就能实现回显了
这个注解使用的时候要注意不能把Bean的名字乱取,尽量保证是类名的首字母小写。

    @ModelAttribute(value="word")
    public void getUser(@RequestParam(value="id",required=false) Integer id,
            Map<String,Object> map){
        if(id!=null){
            map.put("word",wordRepsotory.getById(id));
        }
    }

下面是Handller方法:

    @RequestMapping(value="/user/{id}",method=RequestMethod.GET)
    public String input(@PathVariable("id") Integer id,
            Map<String,Object> map){
        map.put("word",wordRepsotory.getById(id));
        return "jspedit";
    }

3:Dao层
代码中的wordRepsotory是SpringData 自动构建的Dao层,只需要在里面声明一些抽象方法就能帮助项目构建Dao层的查询逻辑代码 附上整个跟Word查询相关Dao层接口:
(建议在使用注解声明查询语句的时候去 MySQL的命令行下去执行一次,可以避免很多不必要的错误)

public interface WordRepsotory extends Repository<Word,Integer>{
    @Query(value="SELECT * FROM word w WHERE w.id = :id", nativeQuery=true)
    Word getById(@Param("id")Integer id);

    @Query(value="SELECT * FROM word ORDER BY RAND() LIMIT 10", nativeQuery=true)
    List<Word> getTenWord();

    @Query(value="SELECT * FROM word", nativeQuery=true)
    List<Word> getAllWord();
}

还有,如果查询有返回值,一定要注意Dao层方法查询的的返回值,因为SpringData帮助我们的项目做了太多的工作,正在使用的时候难免不细心,当数据库实体类多的时候就会不太仔细。(今天一下午就是因为不仔细才弄了好久)
如果不是查询语句,那么就一定要使用事务,也就是要在Service层里面去调用Dao层的查询方法,也就是要使用@Transactional注解 增删改都需要 查询可以直接在Handller里面调用。
举例:保存一个对象

    @Transactional
    public void saveUser(User user){
        userPepsotory.save(user);
    }

还要在Spring的配置文件中配置事务管理器,启用支持事务的注解

    <!-- 3. 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>    
    </bean>

    <!-- 4. 配置支持注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

一名大三码农的结果截图:
点击回显:

这里写图片描述
数据回显:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/XiaoA82/article/details/82192552