第七节——spring中web层快速布置
Posted 想学习安全的小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第七节——spring中web层快速布置相关的知识,希望对你有一定的参考价值。
快速配置
- 导入坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
- 在main-java-demo1-dao目录下创建UserDaoImpl.class模仿数据调用层
public class UserDaoImpl {
public void show(){
System.out.println("qwe");
}
}
- 在main-java-demo1-service目录下创建UserServiceImpl.class建立业务层
public class UserServiceImpl {
private UserDaoImpl userDao;
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public void show(){
userDao.show();
}
}
- 在main-resources目录下创建applicationContext.xml文件,配置bean
<bean id="userDao" class="demo1.dao.UserDaoImpl"/>
<bean id="userService" class="demo1.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
- 在main-java-demo1-web目录下创建UserServlet.class文件,充当控制层
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl userService = (UserServiceImpl) app.getBean("userService");
userService.show();
}
}
- 编辑main-webapp-WEB-INF目录下的web.xml文件配置servlet以及url
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>demo1.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
- 开启tomcat,访问地址:http://localhost:8080/demo02_war/userServlet,看到控制台有输出
以上是关于第七节——spring中web层快速布置的主要内容,如果未能解决你的问题,请参考以下文章