https://github.com/javaobjects/demo_spring03_init_web
demo_spring03_init_web
https://github.com/javaobjects/demo_spring03_init_web
Last synced: 3 months ago
JSON representation
demo_spring03_init_web
- Host: GitHub
- URL: https://github.com/javaobjects/demo_spring03_init_web
- Owner: javaobjects
- Created: 2019-06-19T11:16:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-19T17:54:23.000Z (almost 6 years ago)
- Last Synced: 2025-01-28T03:17:17.291Z (4 months ago)
- Language: Java
- Size: 5.98 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java使用Spring的DI功能改造WEB应用
1. 创建一个动态WEB工程 Dynamic Web Project 并命名为demo_spring03_init_web

2. 搭建环境(导入jar包commons-logging beans context core expression test web)
**添加到libraries路径下面**
成功后会成这样

3. 新建Servlet(controller/UserServlet)

**如图所示Servlet报错**
**解决Servlet报错:**




**如图所示已经解决咯报错**

**删除无用的方法**

**重写Service方法**

**测试是否能够请求到这个Servlet**

启动完成

输入http://localhost:9090/demo_spring03_init_web/UserServlet(备注我的端口是9090)
如图所示,测试成功

3. 新建Service(service/UserService)

```
package service;public class UserService {
public boolean login(String username,String password) {
if("zhangsan".equals(username) && "123456".equals(password)) {
return true;
}else {
return false;
}
}
}
```
4. 拷贝或新建配置文件

**beans.xml**
```
```
**UserServlet**
```
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("welcome!!!!");
//假设:请求传递过来username:password
//那么接收参数
String username = request.getParameter("username");
String password = request.getParameter("password");// UserService userService = new UserService();//之前的写法
//使用userservice不需要servlet自己实例化
//获取web片的spring容器
WebApplicationContext act = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
UserService userService = (UserService) act.getBean("userService");
//调用底层service
boolean result = userService.login(username, password);
//根据调用结果返回响应
response.setContentType("text/html;charset=utf-8");
if(result) {
response.getWriter().write("alert('login success');");
}else {
response.getWriter().write("alert('login fail');");
}
}
```
5. 配置web.xml让spring容器初始化

**web.xml中配置一个侦听器侦听已配置的jar包中的spring-web包中的web.context/ContextloaderListener.css**
```
demo_spring03_init_web
org.springframework.web.context.ContextLoaderListener
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
```
**关联源码** ctrl+鼠标左键跳转

**如图所示关联成功**

**web.xml**
```demo_spring03_init_web
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/beans.xml
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
```
**UserService**
```
package service;
public class UserService {
public UserService() {
System.out.println("userService 实例化了");
}public boolean login(String username,String password) {
if("zhangsan".equals(username) && "123456".equals(password)) {
return true;
}else {
return false;
}
}
}
```
6. 启动项目测试是否配置成功

**如图所示,测试成功**

**拓展知识:** [Eclipse如何修改启动项目与项目名称不一致的情况](https://www.jianshu.com/p/c6961258679a)
7. 带参访问UserServlet
http://localhost:9090/demo_spring03_init_web/UserServlet?username=zhangsan&password=123456

##### 总结
第一步:导入jar包
```
beans context core expression test web commons-logging
```第二步:编写service并配置 编写servlet调用service
```
//获取web版的spring容器
WebApplicationContext act=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
UserService userService=(UserService) act.getBean("userService");
```第三步:在web.xml中注册listener
```
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/beans.xml
```以上就是我对于**Java使用spring的di功能对web应用进行改造** 总结的全部内容,附上[源代码](https://github.com/javaobjects/demo_spring03_init_web)
==================================================================
#### 分割线
==================================================================**博主为咯学编程:父母不同意学编程,现已断绝关系;恋人不同意学编程,现已分手;亲戚不同意学编程,现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏,金额不限。。。**
