快速搭建一个无web.xml的web工程_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 快速搭建一个无web.xml的web工程

快速搭建一个无web.xml的web工程

 2017/12/20 21:56:11  hbxflihua  程序员俱乐部  我要评论(0)
  • 摘要:1、新建一个maven工程;2、pom.xml文件引入jar包,并配置failOnMissingWebXml<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0
  • 标签:Web 一个 XML

?

1、新建一个maven 工程;

?

2、pom.xml文件引入jar包,并配置failOnMissingWebXml?

class="xml" name="code"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.huatech</groupId>
	<version>1.0.0</version>
	<artifactId>web-demo</artifactId>
	<packaging>war</packaging>
	<name>web-demo</name>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>


	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>  
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-war-plugin</artifactId>  
	            <version>2.6</version>  
	            <configuration>  
	                <failOnMissingWebXml>false</failOnMissingWebXml>  
	            </configuration>  
	        </plugin>  
	       
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.0</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			
			
		</plugins>
	</build>
</project>

?

3、以java config方式设置spring mvc;?

package com.huatech.support;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;

public class SimpleWebApplicationInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		ServletRegistration.Dynamic registration = servletContext.addServlet("huatech", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.setInitParameter("contextConfigLocation", "classpath:huatech-servlet.xml");
        registration.addMapping("/");
	}
	
}?

WebApplicationInitializer是Spring MVC提供的接口,可以确保基于代码的配置被探测到、且被自动用于初始化Servlet 3 容器。该接口的一个abstract基类是AbstractAnnotationConfigDispatcherServletInitializer,该abstract基类注册DispatcherServlet更简便,只需要指定映射、列出配置类即可。?

?

?

4、配置huatech-servlet.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" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
     	http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">


	<!-- 扫描@Component注解及其子注解 -->
	<context:component-scan base-package="com.huatech.controller" />

	<mvc:annotation-driven />
	<!--配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

?

5、编写一个IndexController

package com.huatech.controller;

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

@Controller
public class IndexController {

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

?

6、在WEB-INF下新建一个index.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>首页</title>
</head>


<body>
	首页
</body>
</html>

?

附件为demo代码

  • web-demo.zip (5.3 KB)
  • 下载次数: 0
发表评论
用户名: 匿名