spring整合springmvc
- (1)log4j.properties
- (2)web.xml
- (3)springmvc.xml
log4j.properties
不在别的子工程中添加呢?当前 web层的数据的接收与返回,非常重要
log4j.rootLogger=debug, stdout, logfile
log4j.category.org.springframework=info
#log4j.category.org.apache=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=c:\\log\\myweb.log
log4j.appender.logfile.MaxFileSize=1KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--1.spring监听器: 读取applicationContext.xml配置文件-->
<!--修改监听器读取配置路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.字符编码过滤器-->
<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>
<!--3.springmvc前端控制器-->
<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/springmvc.xml</param-value>
</init-param>
<!--项目启动的时候,创建DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
classpath*:spring/applicationContext-*.xml
spring/springmvc.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描Controller所在包-->
<context:component-scan base-package="com.lbl.web"/>
<!--2.视图解析器-->
<!-- success 查找文件 /WEB-INF/pages/success.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--2.1 前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--2.2 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--3.mvc注解驱动-->
<!--3. 把转换器工厂放入到注解驱动,才会生效的 @RequestMapping @ResponseBody @RequestBody-->
<mvc:annotation-driven />
</beans>
CompanyController
/**
* Created by 李柏霖
* 2020/10/22 21:01
*/
package com.lbl.web;
import com.lbl.domain.Company;
import com.lbl.service.ICompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/company")
public class CompanyController {
private static final Logger l = LoggerFactory.getLogger(CompanyController.class);
@Autowired
ICompanyService iCompanyService;
//list.action -> list
@RequestMapping(path="/list.do",method = RequestMethod.GET)
public String list(Model model){
List<Company> list = iCompanyService.findAll();
//打印重要数据
l.info("list list="+list);
model.addAttribute("list",list);
return "company/company-list";
}
}