根據D49中所述,接下來的ssm整合都會用到父子模塊。
注意事項:
service模塊的pom.xml文件中需要導入
<dependency>
<groupId>com.zsl</groupId>
<artifactId>dao_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
同理, web模塊也需要導入service模塊的相關信息。
目的: 依賴傳遞。service模塊可以使用dao模塊的信息,web模塊可以使用service模塊的信息。
搭建環境
web.xml
<!--編寫編碼過濾器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring配置文件(在web.xml中)
<!--dao層開始-->
<!--配置spring的監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!---->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--dao層結束-->
spring-mvc.xml
<!--組件掃描-->
<context:component-scan base-package="com.zsl.controller"/>
<!--mvc註解支持-->
<mvc:annotation-driven />
<!--視圖解析器-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--是否靜態資源-->
<mvc:default-servlet-handler/>
工程的目錄結構
代碼(一部分)
controller:
@RequestMapping(): 請求路徑
@Autowired: 依賴注入(spring-mvc的組件掃描,會掃描到這裏)
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping("/findAll")
public String findAll(Model model) {
List<Product> list = productService.findAll();
System.out.println(11);
//把model放入到request域中
model.addAttribute("list", list);
System.out.println(list);
//指定視圖
return "product-list";
}
ServiceImpl
@Service: 寫在類上,將自動註冊到Spring容器,不需要再在applicationContext.xml文件定義bean了
@Transactional: 開啓事務的註解
@Service
@Transactional //開啓事務的註解
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public List<Product> findAll() {
System.out.println(111111);
return productDao.findAll();
}
dao層
@RequestMapping
public interface ProductDao {
@Select("select * from product")
List<Product> findAll();
}
日期的顯示處理
處理方式
方式一【在頁面上處理】
方式二:在後臺處理
- 凡是日期或者狀態等需要轉換的字段,一般在pojo對象中都有提供兩個屬性,一個做數據庫操作,一個做頁面顯示。
- 後臺
product實體中
public String getDepartureTimeStr(){
return departureTime==null? "暫無出發時間":
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(departureTime);
}
//用來顯示狀態
public String getProductStatusStr(){
return productStatus!=null && productStatus ==1 ? "開啓":"關閉" ;
}
- 前臺頁面
釋放靜態資源
方式一
在springmvc的配置文件中
<!--是否靜態資源-->
<mvc:default-servlet-handler/>
方式二
在springmvc的配置文件中配置
<mvc:resources mapping="" location="" />
方式三
在springmvc的配置文件中什麼都不配置,
在web.xml中的前端控制器攔截的地址配置**.do** 或者**.action**
對日期進行特殊處理
方式一
springmvc默認只能解析2018/01/01 格式的日期,對2018-01-01格式的不能正常解析。
- 在pojo對象中添加註解
方式二
在處理器中添加日期特殊處理
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class,new PropertyValuesEditor(){
/**
* @param text 需要轉換的字符串
* @throws IllegalArgumentException
*/
public void setAsText(@Nullable String text) throws IllegalArgumentException {
Date date=null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
setValue(date);
}
});
}
路徑問題
/*
在html中../表示當前路徑的上一級,
在html中./和不寫表示當前路徑
在html中“${pageContext.request.contextPath}”
==java中的的第一個“/”
==java中的“http://localhost:8080/項目名”
return "redirect:/product/findAll";
*/
return "redirect:findAll";
批量刪除
思路分析
讓springmvc直接封裝一個java數組到後臺。
注意: 如果是form表單內的submit按鈕, 點擊後 表單直接就提交了,在這之前無法做 驗證操作。此刻需要一個單獨的按鈕,點擊按鈕,先驗證,在手動提交表單。
先把id放入一個from表單中
<form id="listForem"
action="${pageContext.request.contextPath}/product/delete">
給批量刪除按鈕綁定點擊事件
<button type="button" class="btn btn-default" title="刪除" onclick=formClick()>
<i class="fa fa-trash-o"></i> 刪除
</button>
<script type="text/javascript">
function formClick() {
//獲取當前複選框
var checkList = $("#dataList td input[type='checkbox']:checked");
//判斷複選框的狀態
if (checkList.size() > 0) {
if (confirm("你確認要刪除嗎?")) {
$("#listForem").submit();
}
} else {
alert("請選擇你要刪除的數據")
}
}
</script>
確認複選框是有值的
後臺代碼
編寫處理器
@RequestMapping("/delete")
public String delete(String[] ids){
productService.delete(ids);
return "redirect:findAll";
}
編寫service
@Override
public void delete(String[] ids) {
for (String id : ids) {
productDao.delete(id);
}
編寫dao
@Delete("delete from product where id = #{id}")
void delete(String id);