详解Spring MVC 构建入门级 Web 应用程序( 二 )

web.xml这最精简的web.xml文件声明了一个Servlet(即dispatcher servlet)来接收所有类型的请求 。Dispatcher servlet在这里充当前端控制器的角色 。
<pre><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee</pre>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Spring Web MVC Hello World Application</display-name><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>spring-servlet.xml(你也可以用applicationContext.xml文件)在请求handler、业务层、dao层,我们使用带注解的类,所以我为“com.howtodoinjava.demo”包里的所有类启用了注解处理
<pre><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee</pre>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Spring Web MVC Hello World Application</display-name><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>EmployeeController.java注解@RequestMapping在类级别和方法级别层面确定将被调用方法的URL 。
<pre><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</pre>http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.howtodoinjava.demo" /><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value=https://www.isolves.com/it/cxkf/yy/JAVA/2019-08-07/"/WEB-INF/views/" />阅读更多:如何使用@Component、@Respository、@Service及@Controller注解?

详解Spring MVC 构建入门级 Web 应用程序

文章插图
 
EmployeeVO.java这个类作为MVC模式的模型 。
<pre>package com.howtodoinjava.demo.model;</pre>import java.io.Serializable;public class EmployeeVO implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; //Setters and Getters @Override public String toString() { return "EmployeeVO [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; }}EmployeeDAO.java这个类位于三层架构中的第三层 。负责与底层的数据库存储进行交互 。
<pre>import java.util.List;</pre>import com.howtodoinjava.demo.model.EmployeeVO;public interface EmployeeDAO{ public List<EmployeeVO> getAllEmployees();}EmployeeDAOImpl.java<pre>import java.util.ArrayList;import java.util.List;</pre>import org.springframework.stereotype.Repository;import com.howtodoinjava.demo.model.EmployeeVO;@Repositorypublic class EmployeeDAOImpl implements EmployeeDAO { public List<EmployeeVO> getAllEmployees() { List<EmployeeVO> employees = new ArrayList<EmployeeVO>(); EmployeeVO vo1 = new EmployeeVO(); vo1.setId(1); vo1.setFirstName("Lokesh"); vo1.setLastName("Gupta"); employees.add(vo1); EmployeeVO vo2 = new EmployeeVO(); vo2.setId(2); vo2.setFirstName("Raj"); vo2.setLastName("Kishore"); employees.add(vo2); return employees; }}EmployeeManager.java这个类处于三层架构中的第二层 。负责与DAO层交互 。
<pre>import java.util.List;
</pre>
import com.howtodoinjava.demo.model.EmployeeVO;
public interface EmployeeManager
{
public List<EmployeeVO> getAllEmployees();


推荐阅读