super.processtemplate
时间: 2023-08-07 19:02:48 浏览: 78
引用[1]中提到了super.processTemplate("edit",request,response),这是一个方法调用,它的作用是将请求转发到edit.html页面。这个方法接受三个参数,第一个参数是字符串"edit",表示要跳转的页面是edit.html;第二个参数是request对象,用于传递请求信息;第三个参数是response对象,用于传递响应信息。通过这个方法,可以实现页面的跳转和数据的传递。[1]
相关问题
springboot freemarker 自定义列表循环标签 带网页标签的示例
下面是一个示例,展示如何在Spring Boot中使用FreeMarker自定义列表循环标签,并带有网页标签:
1. 创建一个自定义标签类CustomListTag,继承LoopTagSupport类,并实现doOnce方法和doEnd方法。
```java
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.utility.DeepUnwrap;
public class CustomListTag extends LoopTagSupport implements TemplateDirectiveModel {
private int index = 0;
@Override
protected boolean process() throws TemplateModelException {
if (index >= size()) {
return false;
}
Environment env = getEnvironment();
TemplateDirectiveBody body = getBody();
if (body != null) {
try {
env.setVariable("index", getStart() + index);
env.setVariable("item", DeepUnwrap.unwrap(get(index)));
body.render(env.getOut());
} catch (IOException | TemplateException e) {
throw new RuntimeException(e);
}
}
index++;
return true;
}
@Override
public void doOnce(Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
super.doOnce(params, loopVars, body);
HttpServletRequest request = (HttpServletRequest) getEnvironment().getCustomAttribute("request");
String contextPath = request.getContextPath();
getEnvironment().setVariable("contextPath", getConfiguration().getObjectWrapper().wrap(contextPath));
}
@Override
public void doEnd(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
throws TemplateException, IOException {
env.setVariable("index", null);
env.setVariable("item", null);
env.setVariable("contextPath", null);
super.doEnd(env, params, loopVars, body);
}
}
```
2. 在Spring Boot配置类中注册自定义标签
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
@Configuration
public class FreeMarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates/");
configurer.setDefaultEncoding("UTF-8");
Map<String, Object> variables = new HashMap<>();
variables.put("customList", new CustomListTag());
configurer.setFreemarkerVariables(variables);
return configurer;
}
}
```
3. 在FreeMarker模板中使用自定义标签
```html
<#list items as item>
<#if customList.first>
<ul>
</#if>
<li>${item}</li>
<#if customList.last>
</ul>
</#if>
</#list>
```
在这个示例中,我们将一个列表包装在自定义标签中,用<ul>和<li>标记包装它,并设置了一个变量contextPath,它包含了上下文路径,在模板中可以方便地使用。
camera2多个createCaptureSession demo
以下是一个简单的 Camera2 API 创建多个 CaptureSession 的示例代码:
```java
public class CameraFragment extends Fragment {
private CameraDevice mCameraDevice;
private CameraCaptureSession mPreviewSession;
private CameraCaptureSession mImageSession;
private Size mPreviewSize;
private Size mImageSize;
private ImageReader mImageReader;
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
mCameraDevice = cameraDevice;
createPreviewSession();
createImageSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
cameraDevice.close();
mCameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
cameraDevice.close();
mCameraDevice = null;
Activity activity = getActivity();
if (null != activity) {
activity.finish();
}
}
};
private final CameraCaptureSession.StateCallback mPreviewSessionCallback =
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
mPreviewSession = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Activity activity = getActivity();
if (null != activity) {
Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
}
}
};
private final CameraCaptureSession.StateCallback mImageSessionCallback =
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
mImageSession = cameraCaptureSession;
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Activity activity = getActivity();
if (null != activity) {
Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
}
}
};
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener =
new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
// Process the captured image
image.close();
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Initialize the camera
openCamera();
}
@Override
public void onResume() {
super.onResume();
// Start the preview session
if (null != mCameraDevice) {
createPreviewSession();
}
}
@Override
public void onPause() {
closeCamera();
super.onPause();
}
private void openCamera() {
Activity activity = getActivity();
if (null == activity || activity.isFinishing()) {
return;
}
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
mImageSize = map.getOutputSizes(ImageFormat.JPEG)[0];
mImageReader = ImageReader.newInstance(mImageSize.getWidth(), mImageSize.getHeight(),
ImageFormat.JPEG, /*maxImages*/2);
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
manager.openCamera(cameraId, mStateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (null != mPreviewSession) {
mPreviewSession.close();
mPreviewSession = null;
}
if (null != mImageSession) {
mImageSession.close();
mImageSession = null;
}
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
if (null != mImageReader) {
mImageReader.close();
mImageReader = null;
}
}
private void createPreviewSession() {
try {
SurfaceTexture texture = getSurfaceTexture();
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(texture);
CaptureRequest.Builder builder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
mCameraDevice.createCaptureSession(Arrays.asList(surface), mPreviewSessionCallback,
null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void createImageSession() {
try {
Surface surface = mImageReader.getSurface();
CaptureRequest.Builder builder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
builder.addTarget(surface);
mCameraDevice.createCaptureSession(Arrays.asList(surface), mImageSessionCallback,
null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void updatePreview() {
if (null == mCameraDevice) {
return;
}
try {
CaptureRequest.Builder builder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
SurfaceTexture texture = getSurfaceTexture();
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(texture);
builder.addTarget(surface);
mPreviewSession.setRepeatingRequest(builder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private SurfaceTexture getSurfaceTexture() {
Activity activity = getActivity();
if (null == activity) {
return null;
}
TextureView textureView = activity.findViewById(R.id.texture_view);
return textureView.getSurfaceTexture();
}
}
```
在此示例中,我们创建了两个 CaptureSession:一个用于预览,一个用于捕获图像。我们使用 ImageReader 来捕获 JPEG 图像,然后在 mOnImageAvailableListener 中处理捕获的图像。在 openCamera() 方法中,我们初始化了 ImageReader 并调用了 manager.openCamera() 来打开相机。在 mStateCallback 的 onOpened() 方法中,我们创建了两个 CaptureSession:一个用于预览,一个用于捕获图像。在 createPreviewSession() 方法中,我们首先获取 SurfaceTexture,然后创建一个 Surface 并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mCameraDevice.createCaptureSession() 来创建预览 CaptureSession。在 createImageSession() 方法中,我们创建了一个与 ImageReader 相关联的 Surface,并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mCameraDevice.createCaptureSession() 来创建捕获图像的 CaptureSession。在 updatePreview() 方法中,我们首先获取 SurfaceTexture,然后创建一个 Surface 并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mPreviewSession.setRepeatingRequest() 来更新预览。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)