无配置文件实现简单web工程

来源:互联网 发布:淘宝耐克双肩包 编辑:程序博客网 时间:2024/05/10 19:58

spring-mvc.xml文件之Java配置类

package com.csl.demo.annotation;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;/** *  * @author Cherry * @date 2017年10月3日 */@Configuration@EnableWebMvc//启用WebMvc@ComponentScan("com.csl.demo.annotation")public class SpringCongfigs {    //注入视图资源解析器    @Bean("viewResolver")    public InternalResourceViewResolver viewResolver(){        //创建        InternalResourceViewResolver irv = new InternalResourceViewResolver();        //设置前后缀//      irv.setPrefix("/webapp/");//      irv.setSuffix(".jsp");        irv.setViewClass(JstlView.class);        return irv;    }}

web.xml文件之WebApplicationInitializer接口类

package com.csl.demo.annotation;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;/** * @author Cherry * @date 2017年10月3日 */public class WebConfig implements WebApplicationInitializer{    /* (non-Javadoc)     * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)     */    @Override    public void onStartup(ServletContext sc) throws ServletException {        //注意:不是AnnotationConfigApplicationContext类        AnnotationConfigWebApplicationContext config = new AnnotationConfigWebApplicationContext();        //配置类注册到web容器        config.register(SpringCongfigs.class);        //设置ServletContext        config.setServletContext(sc);        Dynamic servlet = sc.addServlet("dispatcherServlet", new DispatcherServlet(config));        //添加映射路径        servlet.addMapping("/");        //设置启动顺序        servlet.setLoadOnStartup(1);    }}

@Controller类

/** *  */package com.csl.demo.annotation;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * @author Cherry * @date 2017年10月3日 */@Controllerpublic class HelloController {    @RequestMapping("/hello")    public String hello() {        System.out.println("方法执行了!!");        return "hello.jsp";    }}

pom.xml文件

<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.csl.demo</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <name>demo</name>    <description />    <properties>        <webVersion>3.0</webVersion>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>javax</groupId>            <artifactId>javaee-api</artifactId>            <version>6.0</version>            <scope>provided</scope>        </dependency>        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.3.11.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>4.3.11.RELEASE</version>        </dependency>        <dependency>            <groupId>org.glassfish.web</groupId>            <artifactId>javax.servlet.jsp.jstl</artifactId>            <version>1.2.2</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>2.3.2</version>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>            <plugin>                <artifactId>maven-war-plugin</artifactId>                <version>2.6</version>                <configuration>                    <failOnMissingWebXml>false</failOnMissingWebXml>                </configuration>            </plugin>        </plugins>    </build></project>
阅读全文
0 0
原创粉丝点击