【发布时间】:2016-09-30 23:59:31
【问题描述】:
大家好,我是 Angular 的新手,我正在努力学习它。我开发了一个应用程序,它从 angular 获取输入数据并完成验证,并使用 spring 作为控制器将数据发布到我的服务器。
我的控制器类
@Autowired
UserRegistrationDao userDao;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute("userReg") UserRegistration userReg) {
System.out.println("hi");
userDao.registerUser(userReg);
return new ModelAndView("redirect:/index.jsp");
}
我的示例.js
var app = angular.module('ngMailChimp', []);
app.controller('SignUpController',[ '$scope', '$http', function($scope, $http) {
$scope.list = [];
$scope.headerText = 'AngularJS Post Form Spring MVC example: Submit below form';
$scope.submit = function() {
var formData = {
"firstName" : $scope.ctrl.newCustomer.firstName,
"lastName" : $scope.ctrl.newCustomer.lastName,
"streetName" : $scope.ctrl.newCustomer.streetName,
"aptName" : $scope.ctrl.newCustomer.aptName,
"cityName" : $scopectrl.newCustomer.cityName,
"stateName" : $scope.ctrl.newCustomer.stateName,
"countryName" : $scope.ctrl.newCustomer.countryName,
"zipName" : $scope.ctrl.newCustomer.zipName,
"userName" : $scope.ctrl.newCustomer.userName,
"password" : $scope.ctrl.newCustomer.password,
};
var response = $http.post('save', formData);
response.success(function(data, status, headers, config) {
$scope.list.push(data);
});
response.error(function(data, status, headers, config) {
alert( "Exception details: " + JSON.stringify({data: data}));
});
$scope.list = [];
};
}]);
我的示例.jsp
<body ng-app="ngMailChimp" ng-controller="SignUpController as ctrl">
<div class="signup-wrapper">
<div class="logo">
<img src="resources/assets/images/Untitled.png" alt="Logo"/>
</div>
<div class="alert alert-success message-animation" role="alert" ng-if="ctrl.showSubmittedPrompt">
Thank you! Your account has been created.
</div>
<form name="ctrl.signupForm" ng-submit="submit()">/*ctrl.signup()*/
<div class="form-group" ng-class="{'has-error':ctrl.hasErrorClass('firstName')}">
<label for="firstName"><strong>First Name</strong></label>
<input id="firstName" name="firstName" class="form-control" type="text" required
ng-model="ctrl.newCustomer.firstName" ng-model-options="{ updateOn : 'default blur' }"
ng-focus="ctrl.toggleFirstNamePrompt(true)" ng-blur="ctrl.toggleFirstNamePrompt(false)"/>
<div class="my-messages">
<div class="prompt message-animation" ng-if="ctrl.showFirstNamePrompt">
Please Enter your First Name.
</div>
</div>
<div class="my-messages" ng-messages="ctrl.signupForm.firstName.$error" ng-if="ctrl.showMessages('firstName')">
<div class="message-animation" ng-message="required">
This field is required.<br>
</div>
</div>
<div class="form-group" ng-class="{'has-error':ctrl.hasErrorClass('lastName')}">
<br><label for="lastName"><strong>Last Name</strong></label>
<input id="lastName" name="lastName" class="form-control" type="text" required
ng-model="ctrl.newCustomer.lastName" ng-model-options="{ updateOn : 'default blur' }"
ng-focus="ctrl.toggleLastNamePrompt(true)" ng-blur="ctrl.toggleLastNamePrompt(false)"/>
<div class="my-messages">
<div class="prompt message-animation" ng-if="ctrl.showLastNamePrompt">
Please Enter your Last Name.
</div>
</div>
<div class="my-messages" ng-messages="ctrl.signupForm.lastName.$error" ng-if="ctrl.showMessages('lastName')">
<div class="message-animation" ng-message="required">
This field is required.<br>
</div>
</div>
<button class="btn btn-primary" type="submit">Create My Account</button>
</form>
</div>
we-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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.weber.xxx.controller"></context:component-scan>
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3301/weber" />
<property name="username" value="root" />
<property name="password" value="xxxx" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="userDao"
class="org.weber.xxx.user.registration.dao.UserRegistrationDaoImpl">
<property name="template" ref="jdbcTemplate"></property>
</bean>
web.xml
<servlet>
<servlet-name>weber</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/weber-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>weber</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
它甚至没有击中我的弹簧控制器。我要去哪里磨损g.请帮帮我
谢谢
【问题讨论】:
标签: java angularjs json spring