9.1 在一个简单的web项目中用到spring
实现步骤:
- 创建maven的web项目
- 加入依赖(还要加上jsp和servlet的依赖)
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
- 创建相应代码和配置文件
- 创建jsp发起请求,参数包括id,name,email和age
<body>
<p>学生注册</p>
<form action="reg" method="post">
<table>
<tr>
<td>id</td>
<td> <input type="text" name="id"> </td>
<td>name</td>
<td> <input type="text" name="name"> </td>
<td>email</td>
<td> <input type="text" name="email"> </td>
<td>age</td>
<td> <input type="text" name="age"> </td>
<td>提交</td>
<td> <input type="submit" value="注册"> </td>
</tr>
</table>
</form>
</body>
web.xml
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.sm.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
- 创建servlet,接收请求参数,调用service,调用dao完成注册
在这里项目除了servlet,其他的配置都设置好了,但是这样的话,我们每次提交注册信息,项目都会重新创建新的对象,提交的次数多了,项目就会变得臃肿,内存也可能会不足;所以我们要用到监听器
我们在web.xml中加入
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这样添加监听器后还是会报异常,因为
ContextLoaderListener
被创建对象后会读取/WEB-INF/spring.xml
;为什么要读取文件:监听器创建ApplicationContext
对象需要加载配置文件/WEB-INF/spring.xml
就是监听器读取默认的spring配置文件路径;可以使用<context-param
修改默认的文件位置
<context-param>
<!--contextConfigLocation:表示配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<!--自定义配置路径-->
<param-value>classpath:spring.xml</param-value>
</context-param>
配置监听器:目的是创建容器对象,就能把spring.xml配置文件中的所有对象都创建出来,用户发起请求就可以直接使用对象,不用反复创建了
最终的web.xml
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.sm.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
<context-param>
<!--contextConfigLocation:表示配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<!--自定义配置路径-->
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--注册监听器对象-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在RegisterServlet我们可以用常规方法来创建spring的容器对象
String con= "spring.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(con);
WebApplicationContext ctx=null;
//获取ServletContext中的容器对象,创建好的容器对象,直接拿来用
String key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
Object attr=getServletContext().getAttribute(key);
if(attr != null){
ctx= (WebApplicationContext) attr;
}
但是使用spring自带的方法来创建对象更方便
最终的RegisterServlet
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String strId=request.getParameter("id");
String strName=request.getParameter("name");
String strEmail=request.getParameter("email");
String strAge=request.getParameter("age");
WebApplicationContext ctx=null;
//使用框架提供的方法来获取容器对象
ServletContext sc=getServletContext();
ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
System.out.println("容器对象信息==="+ctx);
//获取service
StudentService ss= (StudentService) ctx.getBean("studentService");
Student student=new Student();
student.setId(Integer.parseInt(strId));
student.setName(strName);
student.setEmail(strEmail);
student.setAge(Integer.valueOf(strAge));
ss.addStudent(student);
//切换页面
request.getRequestDispatcher("/result.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
- 创建jsp作为显示结果页面(略)
演示
首先我们进到注册界面注册第一个学生信息
跳转到注册成功界面
接着我们继续创建第二个学生信息
在这里我们可以看到,注册两个学生的对象信息是一致的,说明项目从头到尾只创建了一个对象,而我们只是反复调用。大家可以对比一下加入监听器之前,每次提交注册信息都会创建一个新的对象,我这里就不演示了,大家可以自己试试~~~
数据库信息添加成功