我正在尝试创建一个简单的 angularjs / springmvc “rest”应用程序...... - 到目前为止,没有成功。
在这个例子中,我想从spring控制器方法中获取一个字符串值 - 即“ getUser()”,然后将其显示在jsp页面上 - 即“针对home.jsp ”。
在浏览器中输入“http://localhost:8084/angtest02/home”后,初始屏幕显示“user:”...
然后,当我将URL修改为“http://localhost:8084/angtest02/home / user ”时 - 即,为了调用spring控制器方法,屏幕仍然只显示“user:”(即,而不是所需的“用户:Joseph Blow ”)...
- 但是,另外,会出现一个对话框,询问:“你想从localhost打开或保存user.json(11个字节)”......
......这不是我想要的。
相反,我的意图是将“ {{user}} ”变量替换为spring控制器方法返回的值:“ getUser()”。 *
下面的图片只是为了说明页面上“{{user}}”变量的位置。
注意 :显然调用了spring控制器方法(即“ getUser()”),但看起来,不是“ userService ”(即位于app.js中)
下面是我的代码......
index.jsp(重定向到home.jsp)
<%
response.sendRedirect("home");
%>
针对home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<title>home page</title>
<meta charset="UTF-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-route.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-resource.js"></script>
<script type="text/javascript" src="resources/js/app.js"></script>
</head>
<body ng-app="mainApp">
<div ng-controller="appController">
<h1>user: {{user}}</h1>
</div>
</body>
</html>
app.js
/* global angular */
var mainApp = angular.module('mainApp', []);
mainApp.controller('appController', function ($scope, userService) {
userService.getUser().success(function (data) {
$scope.user = data;
});
});
mainApp.factory('userService', function ($http) {
var getUser = function () {
return $http.get('/home/user');
};
return {
getUser: getUser
};
});
UserRestController.java
package aaa.bbb.ccc.war;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserRestController
{
private static final org.apache.logging.log4j.Logger LOG = LogManager.getLogger("UserRestController");
@RequestMapping(value = "/home/user", produces = {"application/json"}, method = RequestMethod.GET)
public ResponseEntity<String> getUser()
{
LOG.info("_______getUser()_______entering...");
return new ResponseEntity<>("Joesph Blow", HttpStatus.OK);
}
//***seems I need this "init" method, in order to "land" on the "home.jsp" page, initially...
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(HttpSession session)
{
LOG.info("_______home()_______entering...");
return "home";
}
}
下面的FUTHER DETAILS(如果需要)......
的web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>charEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring/applicationContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
项目文件夹结构如下所示
angtest02
|
+-src
|
+-main
|
+-java
| |
| +-aaa.bbb.ccc.war
| |
| +-UserRestController.java
|
+-resources
| |
| +-log4j2.xml
|
+-webapp
|
+-META-INF
| |
| +-context.xml
|
+-resources
| |
| +-js
| |
| +-app.js
|
+-WEB-INF
|
+-spring
| |
| +-applicationContext.xml
|
+-views
|
+-home.jsp
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="aaa.bbb.ccc.**" />
<mvc:annotation-driven/>
<context:annotation-config/>
<tx:annotation-driven/>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:resources mapping="/webjars/**" location="/webjars/" />
<mvc:resources mapping="/assets/**" location="/assets/" />
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans:beans>
的pom.xml
<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>aaa.bbb.ccc</groupId>
<artifactId>angtest02</artifactId>
<version>1</version>
<packaging>war</packaging>
<name>angtest02</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.3.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>datatables</artifactId>
<version>1.10.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angularjs</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>ng-grid</artifactId>
<version>2.0.14</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:2)
您的控制器正在尝试生成json,但格式似乎不是json。
这可以解决您的问题。
@Controller
public class UserRestController
{
@RequestMapping(value = "/home/user", method = RequestMethod.GET)
public @ResponseBody String getUser()
{
return "Joesph Blow";
}
...
}
或者让控制器成为RestController
@RestController
public class UserRestController{
@RequestMapping(value = "/home/user", method = RequestMethod.GET)
public ResponseEntity<String> getUser()
{
return new ResponseEntity<>("Joesph Blow", HttpStatus.OK);
}
...
}
答案 1 :(得分:1)
已解决:该应用现在看起来像我原先预期的那样......
home.jsp - 更改如下...(使用地图对象... - 不能使用显示简单字符串)...
#!/bin/bash
list=$(printf '%s' {a..z})
getletters(){
local numbers
numbers="$(echo "obase=26; $1"|bc)"
for number in $numbers; do
printf '%s' "${list:10#$number:1}";
done;
echo;
}
count=2
limit=$(( 26**$count - 1 ))
for (( i=0; i<=$limit; i++)); do
printf '%06d %-5.5s\n' "$i" "$(getletters "$i")"
done
app.js - 更改如下......
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<title>home page</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-route.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-resource.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/app.js"></script>
</head>
<body ng-app="mainApp">
<div ng-controller="appController">
<h2>test that requests data from rest controller...</h2>
<p>User: {{user.name}}</p>
</div>
</body>
</html>
UserRestController.java - 更改如下......
/* global angular */
var mainApp = angular.module('mainApp', []);
mainApp.controller("appController", [ '$scope', '$http',
function($scope, $http)
{
$http.get('/angtest02/user.json').
success(function(data) {
$scope.user = data;
});
}]);
applicationContext.xml - 更改如下...
package aaa.bbb.ccc.war;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@Controller
public class UserRestController
{
private static final org.apache.logging.log4j.Logger LOG = LogManager.getLogger("UserRestController");
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = {"application/xml", "application/json"})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Map<String, Object> getUser()
{
LOG.info("_______getUser_______entering...");
//plain string did not work... //String user = "Joseph Blow";
Map<String,Object> user = new HashMap<>();
user.put("name", "Joseph Blow");
return user;
}
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(HttpSession session)
{
LOG.info("_______home_______entering...");
return "home";
}
}