gpt4 book ai didi

java - 想要返回数据到前端,而不是 View - Spring Framework

转载 作者:行者123 更新时间:2023-12-02 11:59:58 27 4
gpt4 key购买 nike

我已经使用 Spring Framework 设置了一个端点。当我访问 URL 时,我的 Controller 受到攻击,它会获取发送的 JSON 数据,并且我可以将内容保存到数据库中。无论成功还是失败,我都想将数据返回到前端,而不是将返回的字符串解析为 View 。如果我返回一个字符串,那么它会尝试转到该页面,因为我看到 ../WEB-INF/views/.html 作为它想去的地方,然后我得到 404。感谢所有帮助!

cc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="
com.classycartridges.controllers,
com.classycartridges.mappers,
com.classycartridges.objects,
com.classycartridges.services" />

<mvc:annotation-driven />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="xxxx" />
<property name="username" value="xxxx" />
<property name="password" value="xxxx" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.classycartridges.objects"/>
<!--<property name="mapperLocations" value="classpath*:com/github/elizabetht/mappers/*.xml" />-->
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.classycartridges.mappers" />
</bean>

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>cc</display-name>
<servlet>
<servlet-name>cc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cc</servlet-name>
<url-pattern>/create</url-pattern>
</servlet-mapping>
</web-app>

帐户 Controller

package com.classycartridges.controllers;

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

import com.classycartridges.objects.Status;
import com.classycartridges.objects.UserAccount;
import com.classycartridges.services.AccountService;

@Controller
public class AccountController {

@Autowired
private AccountService accountService;

@RequestMapping(value = "/create", method = RequestMethod.POST)
public Status createNewUserAccount(UserAccount userAccount) {
//System.out.println("username: " + userAccount.getUsername() + " email: " + userAccount.getEmail() + " password: " + userAccount.getPassword());
String check = accountService.checkForExistingAccount(userAccount);
Status status = new Status();
//if the user email or username already exists, do not persist data
if (("exists").equals(check)) {
status.setStatusText("Error: Account already exists with the specified email or username.");
return status;
} else {
accountService.createAccount(userAccount);
status.setStatusText("Success, your account has been created.");
return status;
}

}

}

最佳答案

给方法添加@ResponseBody注解

@ResponseBody 
@RequestMapping(value = "/create", method = RequestMethod.POST)
public Status createNewUserAccount(UserAccount userAccount) {
...
...
}

关于java - 想要返回数据到前端,而不是 View - Spring Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47344074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com