springmvc搭建(注解)

web.xml添加内容:

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:com/acc/config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
</servlet-mapping>

spring-mvc.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<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"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

        <!-- 激活annotation功能 -->
        <context:annotation-config />
        <!-- 启动 annotation事务 -->
        <mvc:annotation-driven />
        <!-- 导入数据源配置文件 -->
	
        <!-- 启动 IOC注解,扫描指定package下所有注解,并把注解注册为Spring Beans,事务处理-->
        <!--<context:component-scan base-package="com.acc.service.**"/>-->
        <context:component-scan base-package="com.acc.controller.**"/>
        <!-- <context:component-scan base-package="com.acc.entity.**"/> -->
	
	
        <!-- 这样根目录下面的resource的文件(.css,.js等)就不会被spring的DispatchServlet进行过滤-->  
        <mvc:resources location="/resources/" mapping="/resources/**"/>
	
        <!-- 声明视图解析器. <property name="prefix" value="/WEB-INF/view/" /> -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/view/" />
                <property name="suffix" value=".jsp" />
        </bean>
</beans>

Login.java内容:

package com.acc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Login {
        @RequestMapping(value = "/login")
        public String login(){
                return "123";
        }
}

注:123为WEB-INF/view/123.jsp,jsp内容随意

访问路径:http://localhost:8080/accumulation/login.html

相关推荐