简单描述实验一中web工程的文件结构。
com文件夹中放置各层类的代码,详细如下:
本次实验使用的lib包括:
web页面包括:
主要讲解以下四个部分:
domain层中是实体类,实体类包含一些属性及其setter、getter和String方法。
userDao使用Repository注解注入,userService使用ServIce注解注入。
SpringContextListener代码:
package com.servlet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SpringContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
// 定义配置信息
// 配置完web。xml再进行值的填写
String config = context.getInitParameter("contextLocation");
// 获取application context对象
ApplicationContext app = new ClassPathXmlApplicationContext(config);
context.setAttribute("ApplicationContext",app);
}
}
ServletTest3代码:
package com.servlet;
import com.domain.User;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/ServletTest3")
public class ServletTest3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute("ApplicationContext");
UserService userService = app.getBean("userService", UserService.class);
User user = app.getBean("user",User.class);
user.setName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
if(userService.login(user)==1){
// out.println("login successfully...");
// 登录成功 重定向到success.jsp
response.sendRedirect("/spring/success.jsp");
}
else{
// out.println("login fail...");
// 登录失败 转发到error.jsp
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
<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"
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">
<context:component-scan base-package="com">context:component-scan>
<bean id="user" class="com.domain.User">
<property name="name" value="default_name"/>
<property name="password" value="default_password">property>
bean>
beans>
compotent-scan用于注解注入。
user的是xml注入中的set方法注入。
该xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>com.servlet.SpringContextListenerlistener-class>
listener>
<context-param>
<param-name>contextLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Spring Ioc实验title>
head>
<body>
<div align="center">
<form action="ServletTest3" method="POST">
用户名称:<input type="text" name="username">
<br>
密码:<input type="text" name="password" />
<br>
<input type="checkbox" name="auto_name" /> 自动记录名称
<br>
<input type="checkbox" name="auto_psd" /> 自动记录密码
<br>
<input type="submit" value="登录" /> <input type="submit" value="重置" />
form>
div>
body>
html>
html 居中
div align = "center"
method=POST处理POST的HTTP请求;br换行。
不足:
没有使用接口
没有使用Junit测试
小技巧:
Alt+Insert 生成setter、getter和String方法。