千家信息网

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

发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,本篇内容介绍了"如何搭建SpringMVC实现子上下文启动"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所
千家信息网最后更新 2025年01月18日如何搭建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;@Entitypublic 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;@Servicepublic 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;@Repositorypublic class TestUserDao {    public void add(TestUserEntity user){        System.out.println("TestUserDao.add()");    }}
  1. index.jsp

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

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

0