Eclipse+SpringMVC基本項目文件及使用注意事項

1.全部JAR文件(全部spring/logger/jdbc)放到WEB-INF/lib下面,不需要項目中引用(即不需要Build Path)。

基本文件列表:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>spring</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>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config.xml</param-value>
	</context-param> 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	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.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.xsd">
	<context:component-scan base-package="com.spring.controller"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean> 
</beans>

config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
 <bean name="/home" class="com.spring.controller.HomeController"></bean>
 </beans>

 

HomeController.java

package com.spring.controller;

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.servlet.ModelAndView;

@Controller
public class HomeController { 
	@RequestMapping("/home")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello Model View"); 
		return mv;
	}
}

home.jsp 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
hello ,I am home.${msg}
</body>
</html>

 

2.web.xml 裏 寫上:
    <absolute-ordering/> 
</web-app>

解決報重複項目的問題。

3.index.jsp index.html要放這WebContent要目錄下,不是WEB-INF目錄下,這個目錄是受保護的。

4.有xml頭問題點eclipse下project->clean解決。

5.解決POST數據中文問題:

<filter>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>utf-8</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  

6. 設置網站的默認頁面:

a)第一種方式:在springmvcmybatis-servlet.xml 設置 mvc:view-controller 這個標籤

首先要建好:/WEB-INF/jsp/index.jsp

第二種方式:在 Controller的方法前加上: RequestMapping("/")

@RequestMapping("/")
	public String index() {
		return "index";
	}

即可。

 

7.從SVN中檢出的項目,如果無法運行tomcat,則在開始時新建一個Server,即可。

 

Mac 下eclipse 打開文件亂碼的解決方法:https://blog.csdn.net/goodpress/article/details/7819026

Eclipse 中沒有動態Web的解決方法:https://blog.csdn.net/qq_35556254/article/details/91906003

 

8.Eclipse 與SVN集成:在 eclipse -> help -> eclipse marketplace 搜索 :subclipse 並安裝,然後在項目右鍵->Team->share.

https://www.cnblogs.com/liangguangqiong/p/7965770.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章