MVC架构Maven项目(五)Spring集成

1.web.xml配置

<!-- 
    第一个*代表不仅在class路径查找,还会在jar中的class路径查找;
    第二**代表class路径下的任意目录
    第三个*任意以application-context开头的文件
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/application-context*.xml</param-value>
</context-param>

<!-- Spring Configuration Spring配置 -->
<listener>
    <listener-class>        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

2.application-context.xml配置

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- 隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor -->
    <context:annotation-config/> 

    <context:component-scan base-package="com.anonymity.mobile.blog.*"/>     
</beans>

3.Service类

package com.anonymity.mobile.blog.service;

import java.util.List;

import com.anonymity.mobile.blog.entity.Users;

public interface UserService {

    /**
     * 用户列表
     * @return
     */
    public List<Users> userList();
}

4.ServiceImpl类

package com.anonymity.mobile.blog.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.anonymity.mobile.blog.entity.Users;
import com.anonymity.mobile.blog.service.UserService;

@Component("userServcie")
public class UserServiceImpl implements UserService {

    /**
     * 用户列表
     * @return
     * @see com.anonymity.mobile.blog.service.UserService#userList()
     */
    @Override
    public List<Users> userList() {
        List<Users> list = new ArrayList<Users>();
        Users u1 = new Users();
        u1.setUserName("admin");
        u1.setPwd("000000");
        Users u2 = new Users();
        u2.setUserName("guest");
        u2.setPwd("000000");
        list.add(u1);
        list.add(u2);
        return list;
    }
}

5.JSP页面

<%@page import="java.util.*"%>  
<%@page import="com.anonymity.mobile.blog.entity.*"%>  
<html>
<body>
<%
    List<Users> userList = (List<Users>)request.getAttribute("userList");
    for(Users user:userList){
        out.println("<h1>"+user.getUserName()+"</h1>");
    }
%>
</body>
</html>

6.备注

6.1调整web.xml配置
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
6.2调整dispatch-servlet.xml配置
<!--
<mvc:resources mapping="/WEB-INF/jsp/demo/**" location="/WEB-INF/jsp/demo/" cache-period="31556926"/>
-->
InternalResourceViewResolver将视图名解析为一个url,可以理解为转发获取jsp页面。如果使用/*拦截,则需要配置
<mvc:resources mapping="/WEB-INF/jsp/demo/**" location="/WEB-INF/jsp/demo/" cache-period="31556926"/>标签或者 <mvc:default-servlet-handler/>,就可以访问静态资源了。但是这样的话就显示jsp源码了,并没有解析为servlet。所以使用*.html拦截。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值