我已经使用 Spring Framework 设置了一个端点。 当我访问我的 URL 时,我的控制器被击中,它会获取发送的 JSON 数据,我可以将内容保存到数据库中。 无论成功还是失败,我都想将数据返回到前端,而不是将返回的字符串解析为视图。 如果我返回一个字符串,那么它会尝试转到我看到的该页面../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>
网络.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>
帐户控制器
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) {
...
...
}