2.Spring MVC实现代码

2021-07-03 05:07

阅读:379

标签:ade   内存   dai   param   http   https   maven   entity   work   

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/maven-v4_0_0.xsd">
    modelVersion>4.0.0modelVersion>
    groupId>com.my.dailyUtilsgroupId>
    artifactId>dailyUtilsartifactId>
    packaging>warpackaging>
    version>0.0.1-SNAPSHOTversion>
    name>dailyUtilsname>
    url>http://maven.apache.orgurl>
    build>
        finalName>dailyUtilsfinalName>
        plugins>
            plugin>
                groupId>org.apache.maven.pluginsgroupId>
                artifactId>maven-compiler-pluginartifactId>
                version>3.3version>
                configuration>
                    source>1.8source>
                    target>1.8target>
                configuration>
            plugin>
        plugins>
    build>
    properties>
        spring.version>4.2.5.RELEASEspring.version>
    properties>
    dependencies>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.12version>
            scope>testscope>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-contextartifactId>
            version>${spring.version} version>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-context-supportartifactId>
            version>${spring.version}version>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-ormartifactId>
            version>${spring.version}version>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-aopartifactId>
            version>${spring.version}version>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webartifactId>
            version>${spring.version}version>
        dependency>
        dependency>
            groupId>org.springframeworkgroupId>
            artifactId>spring-webmvcartifactId>
            version>${spring.version}version>
        dependency>
        
        dependency>
            groupId>org.aspectjgroupId>
            artifactId>aspectjrtartifactId>
            version>1.6.11version>
        dependency>
        
        dependency>
            groupId>org.aspectjgroupId>
            artifactId>aspectjweaverartifactId>
            version>1.6.11version>
        dependency>
        dependency>
            groupId>javax.servletgroupId>
            artifactId>javax.servlet-apiartifactId>
            version>3.0.1version>
            scope>providedscope>
        dependency>
        dependency>
            groupId>javax.servlet.jspgroupId>
            artifactId>jsp-apiartifactId>
            version>2.1version>
            scope>providedscope>
        dependency>
    dependencies>
project>

2.web.xml

xml version="1.0" encoding="UTF-8"?>
web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    display-name>Daily Utils Applicationdisplay-name>
    
  
  servlet>
    servlet-name>springmvcservlet-name>
    servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    init-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath:config/springmvc.xmlparam-value>
    init-param>
    load-on-startup>1load-on-startup>
  servlet>
 
 
  servlet-mapping>
    servlet-name>springmvcservlet-name>
    
    url-pattern>/url-pattern>
  servlet-mapping>    

    
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath:config/app-context.xmlparam-value>
    context-param>

    
    listener>
        listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    listener>
        listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
    listener>
    
    listener>
        listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>
    
    
    welcome-file-list>
        welcome-file>/welcome.jspwelcome-file>
    welcome-file-list>
     
web-app>

2.springmvc.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
    
    

    
    

    
      
    mvc:annotation-driven />
    context:annotation-config />
    mvc:default-servlet-handler />

    
    

    
    
    context:component-scan base-package="com.my.controller" />

    
    bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        property name="prefix" value="/WEB-INF/js/" />
        property name="suffix" value=".jsp" />
        property name="contentType" value="text/html;charset=UTF-8" />
    bean>
beans>

4.app-context.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    context:annotation-config />    
    
    bean id="bird" class="com.my.entity.Bird" scope="singleton"
        init-method="initBean" destroy-method="destroy" autowire="byName">
        property name="bridName" value="<>" />
        property name="color">
            value>]]>value>
        property>
    bean>
beans>

5.Handler

 

package com.my.controller;

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

@Controller
@RequestMapping(value = "/next")
public class ModelController {
    
    @RequestMapping(value="/more")
    public ModelAndView returnModel(){
        ModelAndView model = new ModelAndView();
        model.setViewName("index"); 
     model.addObject("message", "This is a test!");
return model; } }

 

或者

package com.my.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "/test")
    public String recruitdemand(final HttpServletRequest request, final HttpServletResponse response) {
        request.setAttribute("message", "This is a test!");return "index";
    }

}

7.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>indextitle>
head>
body>
h2>${message}h2>
body>
html>

 

 

 

Daily Utils Application        springmvc    org.springframework.web.servlet.DispatcherServlet                contextConfigLocation        classpath:config/springmvc.xml        1          springmvc        / 
contextConfigLocationclasspath:config/app-context.xml
org.springframework.web.context.ContextLoaderListenerorg.springframework.web.context.request.RequestContextListenerorg.springframework.web.util.IntrospectorCleanupListener/welcome.jsp 

2.Spring MVC实现代码

标签:ade   内存   dai   param   http   https   maven   entity   work   

原文地址:https://www.cnblogs.com/lukelook/p/9626937.html


评论


亲,登录后才可以留言!