您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
10-Spring练习-角色列表展示-配置实现
发布时间:2025-01-07 23:02:03编辑:雪饮阅读()
-
继上篇,接下来我们首先需要控制器变成真正意义的控制器,需要用controller注解,并将其属性RoleService类型的roleService也自动注解而不需要setter方法
所以我们的控制器将变成这样:
package sp21.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import sp21.domain.Role;
import sp21.service.RoleService;
import java.util.List;
@RequestMapping("/role")
@Controller
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView modelAndView=new ModelAndView();
List<Role> roleList=roleService.list();
modelAndView.addObject("rolelist",roleList);
modelAndView.setViewName("role-list");
return modelAndView;
}
}
既然通过controller注解实现控制器,那么接下来需要用包扫描,所以需要spring mvc配置文件增加context名称空间及对应的schemaLocation并新增controller的包扫描路径,所以spring mvc配置文件完整如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--mvc注解驱动-->
<mvc:annotation-driven/>
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--静态资源权限开放-->
<mvc:default-servlet-handler/>
<context:component-scan base-package="sp21.controller"/>
</beans>
那么我们的控制器里面还用到roleService,而且用Autowired注解进行了自动注入。
所以我们还需要在Spring配置文件中增加roleService的bean,那么我们的roleService里面又包含roleDao,而roleDao里面又包含jdbcTemplate。所以最终我们的Spring配置文件完整如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--加载properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置roleDao-->
<bean id="roleDao" class="sp21.dao.impl.RoleDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--配置roleService-->
<bean id="roleService" class="sp21.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
</bean>
</beans>
关键字词:Spring,配置,角色列表