java开发框架之SSM整合框架( 四 )


</context-param>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决中文乱码的过滤器-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、测试SpringMVC的框架搭建是否成功
1. 编写index.jsp和list.jsp编写,超链接
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>查询所有!</h2>
<a href=https://www.isolves.com/it/cxkf/kj/2021-11-11/"account/findAll">查询所有
<a href=https://www.isolves.com/it/cxkf/kj/2021-11-11/"account/findAll">测试查询
<h3>测试包</h3>
<form action="account/save" method="post">
姓名:<input type="text" name="name" /><br/>
金额:<input type="text" name="money" /><br/>
<input type="submit" value=https://www.isolves.com/it/cxkf/kj/2021-11-11/"保存"/>

</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>查询所有的帐户</h3>
<c:forEach items="${list}" var="account">
${account.name}
</c:forEach>
</body>
</html>
2、创建AccountController类,编写方法,进行测试
package com.mingqi.controller;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
/**
* 查询所有数据
* @return
*/
@RequestMapping("/findAll")
public String findAll(Model model)
{
System.out.println("表现层查询所有");
accountService.findAll();
List<Account> list = accountService.findAll();
model.addAttribute("list",list);
return "list";
}
@RequestMapping("/save")
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException
{
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}
}
配置tomcat,启动项目,进行测试

java开发框架之SSM整合框架

文章插图
推荐阅读:
java开发之Java 虚拟机栈
Java开发中SSM框架整合开发之 Spring IoC
Java开发框架之Spring JDBC




推荐阅读