💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
一.**pom.xml代码** ~~~ <?xml version="1.0" encoding="UTF-8"?> <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>org.example</groupId> <artifactId>springmvc2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc2 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.9.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- 引入json包 开始--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.10.3</version> </dependency> <!-- 引入json包 结束--> </dependencies> <build> <finalName>springmvc2</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project> ~~~ ***** 二.**web.xml代码** ~~~ <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置解决中文乱码过滤器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> ~~~ ***** 三.**springmvc.xml**代码 ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启注解扫描--> <context:component-scan base-package="com.youge"></context:component-scan> <!-- 视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置自定义类型转换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.youge.utils.StringToDateConverter"/> </set> </property> </bean> <!-- 前端控制器,哪些静态资源不拦截--> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <!-- 开启SpringMVC框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService" /> </beans> ~~~ ***** 四.**response.jsp**代码 ~~~ <%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-09-26 Time: 13:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> <script src="/js/axios.min.js"></script> <script src="/js/vue.min.js"></script> </head> <body> <a href="/user/testString">testString</a><br> <a href="/user/testString1">testString1</a><br> <a href="/user/testVoid">testVoid</a><br> <a href="/user/testModelAndView">testModelAndView</a><br> <a href="/user/testForwardOrRedirect">testForwardOrRedirect</a><br> <div id="app"> <a @click="showMsg">弹出窗口</a><br> <button @click="testAjax">testAjax</button> </div> <br>${user.age} <script> var app=new Vue({ el:'#app', methods:{ showMsg:function (){ alert('弹出窗口!') }, testAjax(){ axios.post('/user/testAjax',{ username:"张三", password:'123456', age:30 }).then((res)=>{ console.log(res); console.log(res.data); console.log(res.data.username); }).catch() } } }); </script> </body> </html> ~~~ ***** 五.**User.java**代码 ~~~ package com.youge.domain; import java.io.Serializable; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-26 14:26 */ public class User implements Serializable { private String username; private String password; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } } ~~~ ***** 六.**UserController.java**代码: ~~~ package com.youge.controller; import com.youge.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-26 14:12 */ @Controller @RequestMapping("/user") public class UserController { /** * 返回类型 String * * @param model * @return */ @RequestMapping("/testString") public String testString(Model model) { System.out.println("testString方法执行了..."); //模拟从数据库中查询出User对象 User user = new User(); user.setUsername("乔峰"); user.setPassword("123456"); user.setAge(25); //model对象 model.addAttribute("user", user); return "success"; } @RequestMapping("/testString1") public String testString1() { System.out.println("testString1执行了...."); return "success1"; } /** * 返回类型 void * 一.请求转发: 一次请求,不用写项目的名称 * 二.重定向: 两次请求 */ @RequestMapping("/testVoid") public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("testVoid执行了..."); //一.请求转发 // request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response); //二.重定向 // response.sendRedirect(request.getContextPath()+"/index.jsp"); //三.直接进行响应 //解决中文乱码 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println("中文..."); return; } /** * 返回类型 modelAndView * @return */ @RequestMapping("/testModelAndView") public ModelAndView testModelAndView() { System.out.println("testModelAndView方法执行了..."); //创建ModelAndView对象 ModelAndView mv = new ModelAndView(); //模拟从数据库中查询出User对象 User user = new User(); user.setUsername("段誉"); user.setPassword("987654321"); user.setAge(22); //把user对象存储到mv对象中,也会把user对象存入到request对象 mv.addObject("user",user); //跳转到哪个页面 mv.setViewName("success"); return mv; } /** * 使用关键字的方式进行转发或者重定向 * @return */ @RequestMapping("/testForwardOrRedirect") public String testForwardOrRedirect(){ System.out.println("testForwardOrRedirect执行了...."); //请求转发 // return "forward:/WEB-INF/pages/success.jsp"; //重定向 return "redirect:/pages/index.jsp"; } /** * ajax测试 * @param body */ // @RequestMapping("/testAjax") // public void testAjax(@RequestBody String body){ // System.out.println("testAjax执行了..."); // System.out.println(body); // } /** * ajax测试,响应json格式数据 * @param user */ @RequestMapping("/testAjax") public @ResponseBody User testAjax(@RequestBody User user){ System.out.println("testAjax执行了..."); System.out.println(user); //做响应,模拟查询数据库 user.setUsername("李四"); user.setAge(35); return user; } } ~~~