如何搭建SpringMVC实现子上下文启动

本篇内容介绍了“如何搭建SpringMVC实现子上下文启动”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联专注于临沭网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供临沭营销型网站建设,临沭网站制作、临沭网页设计、临沭网站官网定制、小程序制作服务,打造临沭网络公司原创品牌,更为您提供临沭网站排名全网营销落地服务。

  1. pom.xml


  4.0.0
  com.hqt.toy
  toy01
  0.0.1-SNAPSHOT
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    6
                    6
                
            
        
    
    war
  
  
  	4.3.7.RELEASE
	8.0.28
  

  
	  
	  
		  javax.servlet
		  javax.servlet-api
		  3.0.1
	  
	  
	  
		  javax.servlet.jsp
		  javax.servlet.jsp-api
		  2.3.1
	  
	  
	  
		  org.springframework
		  spring-webmvc
		  ${spring.version}
	  

      
      
          org.hibernate.javax.persistence
          hibernate-jpa-2.0-api
          1.0.1.Final
      

	
        
            org.apache.tomcat.embed
            tomcat-embed-core
            ${tomcat.embed.version}
        

        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            ${tomcat.embed.version}
            
        
        
            org.apache.tomcat.embed
            tomcat-embed-logging-juli
            ${tomcat.embed.version}
        

        
            org.apache.commons
            commons-lang3
            3.8
        
  
  1. web.xml



    
        
        springmvc
        
            org.springframework.web.servlet.DispatcherServlet
        
        
        
            contextConfigLocation
            classpath:/dispatcher-servlet.xml
        
        
        1
    
    
        springmvc
        *.do
    
  1. dispatcher-servlet.xml




    
    

    
    

    
    
  1. TestUserController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test_user.do")
public class TestUserController{
    @Autowired
    private TestUserService userService;

    @RequestMapping(params="method=user")
    public String user(TestUserEntity user, String uname, ModelMap modelMap){
        System.out.println("获取前台的值:"+user.getUname());
        System.out.println("String uname:"+uname);
        modelMap.addAttribute("newname", user.getUname());
        userService.add(user.getUname());
        return "index";
    }
}
  1. TestUserEntity(非必须)

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class TestUserEntity {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String uname;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
}
  1. TestUserService(非必须)

import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class TestUserService {
    @Resource
    private TestUserDao userDao;

    public void add(String uname) {
        System.out.println("UserService.add()");
        TestUserEntity user = new TestUserEntity();
        user.setUname(uname);
        userDao.add(user);
    }
}
  1. TestUserDao(非必须)

import org.springframework.stereotype.Repository;

@Repository
public class TestUserDao {
    public void add(TestUserEntity user){
        System.out.println("TestUserDao.add()");
    }
}
  1. index.jsp


    用户名:
          返回的用户名:${newname}

“如何搭建SpringMVC实现子上下文启动”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


标题名称:如何搭建SpringMVC实现子上下文启动
分享网址:http://scyanting.com/article/gpdcsi.html