Spring整合SpringMVC

来源:互联网 发布:手机常用必备软件 编辑:程序博客网 时间:2024/06/13 03:08
1.整个项目的架构图和JAR包如下:
架构图

jar包



2.web.xml文件的配置
<?xml version="1.0"encoding="UTF-8"?><web-appxmlns="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_3_1.xsd"version="3.1"><!--加载Spring的配置文件--><context-param><param-name>contextConfigLocation</param-name><!--applicationContext.xml的路径--><param-value>classpath:applicationContext.xml</param-value></context-param><!--配置Spring的监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--配置Spring MVC的拦截器--><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--按照SpringMVC的规定如果SpringMVC的配置文件的名字是 [Servletname]-servlet.xml并且放在 WEB-INF文件夹下 则不需要设置路径否则需要在init-param中设置配置文件的路径--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:dispatcher-servlet.xml</param-value></init-param><!--容器启动即加载--><load-on-startup>1</load-on-startup></servlet><!--配置拦截器需要拦截的url后缀 /*为都拦截--><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping></web-app>



3.applicationContext.xml文件的配置
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--配置Spring所要扫描的注解 @Controller注解交给SpringMVC来处理 Spring不处理--><context:component-scanbase-package="com.star.*"><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>



4.配置SpringMVC的dispatcher-servlet.xml文件的配置
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--配置仅扫描Controller注解的类 默认为都扫描需要吧 use-default-filters设置为false--><context:component-scanbase-package="com.star.*"use-default-filters="false"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><!--配置Spring MVC的视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--前缀--><propertyname="prefix"value="/WEB-INF/"/><!--后缀--><propertyname="suffix"value=".jsp"/></bean><mvc:annotation-driven/></beans>



5.配置action类 TestHandlber.java
<pre name="code" class="java">package com.star.Handlber;import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;/** * Created by Star on 2016/9/16. * * @author star * @version 1.0 */@Controller@RequestMapping("/login")public class TestHandlber {    @RequestMapping("/login")    public String login(@RequestParam(value = "username",required = false)String username){        //获取Logger对象        Logger logger = Logger.getRootLogger();        logger.info("开始验证用户名!");        if(username.equals("admin")){            logger.info("登陆成功!用户名为:"+username+"--重定向到success.jsp页面");            return "success";        }else {            logger.info("登陆失败!用户名不正确!跳转到error页面!");            return "error";        }    }}





6.配置登陆成功页面success.jsp
<%--Created by IntelliJ IDEA.User: StarDate: 2016/9/16Time: 19:22To change this template use File | Settings | File Templates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java" %><html><head><title>success</title></head><body><h1style="color:green">success</h1></body></html>



7.配置错误页面error.jsp
<%--Created by IntelliJ IDEA.User: StarDate: 2016/9/16Time: 19:22To change this template use File | Settings | File Templates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java" %><html><head><title>error</title></head><body><h1style="color:red">error</h1></body></html>



8.log4j.properties
log4j.rootLogger=DEBUG, Console#Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG


1 0