struts2作为一个mvc框架,请求一个方法,返回一个页面,当post请求成功之后,跳转到success页面,大多数情况下能够满足我们的要求,但是web开发中ajax技术的使用是不可避免的,我们知道ajax请求大多数情况下需要的数据是json格式。我们如何让struts这个mvc框架作为一个restful服务器,提供rest api,显得尤为重要,虽然这时候不能完全体现mvc框架的职能。
struts2实现json请求,可以有两种方式:
第一种:通过struts2-json-plugin插件,这种方式需要在struts.xml中配置action,而且package需要继承json-default,另外还需要配置结果资源。
第二种:传统的输出流,向ajax请求写结果。
第一种方式,如果是maven工程,那么就需要在pom.xml中加入struts2-json-plugin依赖。
注意:struts2-json-plugin插件版本最好与struts版本一致
第一种方式,如果是maven工程,那么就需要在pom.xml中加入struts2-json-plugin依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.24</version>
</dependency>
项目结构如下:
其余文件如下:
导包:
User实体类:
package cn.tedu.entity;
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String username;
private String password;
...
}
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_2_5.xsd" version="2.5">
<display-name>struts_day03</display-name>
<filter>
<display-name>StrutsPrepareAndExecuteFilter</display-name>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</context-param>
</web-app>
spring-web.xml如下:(开启组件扫描)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- spring的组件扫描 -->
<context:component-scan base-package="cn.tedu"/>
</beans>
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package namespace="/demo" name="demo" extends="struts-default,json-default">
<!-- 返回json字符串 -->
<action name="json11" class="jsonAction" method="json1">
<result name="success" type="json">
<!-- struts2在IE中会提示下载,而不返回json字符串,需要配置该选项 -->
<param name="contentType">text/html</param>
<param name="root">user</param>
</result>
</action>
</package>
</struts>
JsonAction如下:
package cn.tedu.json;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.tedu.entity.User;
import cn.tedu.web.BaseAction;
//struts2中实现返回json格式请求
@Controller
@Scope("prototype")
public class JsonAction extends BaseAction{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String json1(){
user = new User();
user.setId(1);
user.setUsername("lily");
user.setPassword("123456");
return SUCCESS;
}
}
URL:http://localhost:8000/struts_test2/demo/json11
页面显示结果:{"user":{"id":1,"password":"123456","username":"莉莉丝"}}
该方式在谷歌 和 IE11 和 Edge中皆可正常使用
注意:
(1)struts2在IE中会提示下载,而不返回json字符串,需要配置如下选项
<param name="contentType">text/html</param>
(2)Action中要有User的GET和SET方法
(3)在配置struts.xml文件时,有3个地方需要注意:
- package需要继承json-default,中间用逗号隔开
- result的type值是json
- param名为wq的参数就是我们在 action中定义的user变量
(4)请求确实返回了json格式数据,而且json编码是utf-8。可以支持中文