你的位置:首页 > 软件开发 > Java > SpringMVC基础框架搭建

SpringMVC基础框架搭建

发布时间:2016-01-20 21:00:17
SpringMVC框架搭建步骤: 1、将需要jar包导入lib文件夹下 2、配置web.说明:本项目源码导入eclipse,在tomcat运行后 输入http://localhost:8080/BrainTrain/w ...

SpringMVC框架搭建步骤:        

  1、将需要jar包导入lib文件夹下   2、配置web.

说明:本项目源码导入eclipse,在tomcat运行后 输入http://localhost:8080/BrainTrain/welcome.jsp进行测试

所需的jar包:http://pan.baidu.com/s/1i3QKYNF(百度云盘)

项目源码:http://pan.baidu.com/s/1kTWM9Rh​;(百度云盘)

一、配置web.

<?

<web-app

  <display-name>BrainTrain</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <servlet>

  <servlet-name>springMVC</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <init-param>

  <param-name>contextConfigLocation</param-name>

  <!--springMVC配置文件地址,config是src下的包  -->

  <param-value>classpath*:config/springAnnotation-servlet.

  </init-param>

  <load-on-startup>1</load-on-startup>

  </servlet>

 

  <servlet-mapping>

  <servlet-name>springMVC</servlet-name>

  <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

​二、配置springMVC核心配置文件:springAnnotation-servlet.

<?

<beans

 

 

 

 

 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.xsd 

      http://www.springframework.org/schema/mvc 

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    

<!-- 注解扫描包 -->

<context:component-scan base-package="com.chuck.codeResource"></context:component-scan>

   

     <!-- 开启注解 -->

<mvc:annotation-driven/>

<!-- 静态资源访问 -->

<mvc:resources location="/img/" mapping="/img/**"/> 

<mvc:resources location="/js/" mapping="/js/**"/>

<mvc:resources location="/staticHtml/" mapping="/staticHtml/**"/>

<bean id="viewResolver" >

<property name="prefix" value="/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

 </beans> 

三、编写Controller类

package com.chuck.codeResource.user;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller

@RequestMapping("/user")

public class LoginController extends MultiActionController {

@RequestMapping("/addUser")

    public String addUser(HttpServletRequest request,HttpServletResponse response){

    System.out.println("-----add----");

    String result="this is addUser";

    request.setAttribute("result",result);

    return "/welcome";

    }

@RequestMapping("/delUser")

    public String delUser(HttpServletRequest request,HttpServletResponse response){

    System.out.println("-----delUser----");

    String result="this is delUser";

    request.setAttribute("result",result);

    return "/welcome" ; 

    }

@RequestMapping("/updateUser")

    public String updateUser(HttpServletRequest request,HttpServletResponse response){

    System.out.println("-----update----");

    String result="this is updateUser";

    request.setAttribute("result",result);

    return "/welcome";

  

    }

}

四、配置访问的jsp页面:welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

    本次执行的方法是:${result}

    <form action="user/addUser" method="post">

       <input type="submit" value="post"/>

    </form>

</body>

</html>


原标题:SpringMVC基础框架搭建

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录