我已经设置了如下web.xml.我还有一个基于注释的控制器,它接受任何URL模式,然后转到相应的jsp(我在-servlet.xml中设置了它).但是,如果我转到以.html结尾的页面(并且其jsp不存在),我看不到自定义404页面(并在日志中看到以下错误).任何不以.html结尾的页面,我都可以看到自定义404页面.
如何为通过DispatcherServlet的任何页面配置自定义404页面?
还想添加,如果我将我的错误页面设置为静态页面(即.html.htm)它可以工作,但如果我将其更改为jsp(即错误.jsp),我会得到IllegalStateException.任何帮助,将不胜感激.
记录错误
Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:606)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
Run Code Online (Sandbox Code Playgroud)
调节器
@RequestMapping(value = {"/**"})
public ModelAndView test() {
ModelAndView modelAndView = new ModelAndView();
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
web.xml中
<servlet>
<servlet-name>my_servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
Run Code Online (Sandbox Code Playgroud)
...
<servlet-mapping>
<servlet-name>my_servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
...
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud) 在Java 5之前,没有注释.因此,您无法向类添加元数据.
要将类标记为可序列化,您必须实现Serializable接口(这只是一个标记)并使用更多transient关键字将字段标记为不可序列化(如果需要),例如:
public class MyClass implements Serializable {
...
private transient Bla field;
...
}
Run Code Online (Sandbox Code Playgroud)
现在你理论上可以使用注释(这对他们来说是完美的用途)并且具有:
@Serializable
public class MyClass {
...
@Transient
private Bla field;
...
}
Run Code Online (Sandbox Code Playgroud)
但是不推荐使用接口和关键字,并且在Java 5中没有添加注释来替换它们.
保留界面和关键字的决定有哪些考虑因素?
当然,存在与Java 5之前的代码兼容的问题,但在某些时候它将结束(例如,与泛型的新功能相关,JLS指定未来版本的Java编程语言可能会禁止使用原始类型).那么为什么不为序列化注释做准备呢?
有什么想法吗?(虽然我更喜欢具体的参考文献:D 我无法找到)
我想提供注释与一些方法生成的一些值.
到目前为止我试过这个:
public @interface MyInterface {
String aString();
}
Run Code Online (Sandbox Code Playgroud)
@MyInterface(aString = MyClass.GENERIC_GENERATED_NAME)
public class MyClass {
static final String GENERIC_GENERATED_NAME = MyClass.generateName(MyClass.class);
public static final String generateName(final Class<?> c) {
return c.getClass().getName();
}
}
Run Code Online (Sandbox Code Playgroud)
思想GENERIC_GENERATED_NAME是static final,它抱怨说
注释属性的值
MyInterface.aString必须是常量表达式
那么如何实现呢?
我在我的数据库3个表:Students,Courses和Students_Courses
学生可以有多门课程,课程可以有多个学生.之间存在许多一对多的关系Students和Courses.
我有3个案例用于我的项目,课程已添加到我的Courses表格中.
User_Courses- 再次,预期的行为.Students和Students_Courses,但它也删除Courses其中不需要的记录.即使我在课程中没有任何用户,我也希望课程能够在那里.下面是我的表和注释类的代码.
CREATE TABLE `Students` (
`StudentID` INT(11) NOT NULL AUTO_INCREMENT,
`StudentName` VARCHAR(50) NOT NULL
PRIMARY KEY (`StudentID`)
)
CREATE TABLE `Courses` (
`CourseID` INT(11) NOT NULL AUTO_INCREMENT,
`CourseName` VARCHAR(50) NOT NULL
PRIMARY KEY (`CourseID`)
)
CREATE TABLE `Student_Courses` (
`StudentId` INT(10) NOT NULL DEFAULT '0',
`CourseID` INT(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`StudentId`, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Butter Knife来简化自定义BaseAdapter类的创建.我在这里的例子如下:http://jakewharton.github.io/butterknife/在"另一个用途是简化列表适配器内的视图持有者模式".部分.不幸的是,每次为列表中的每个项目创建ViewHolder时,我都会收到"无法注入视图"错误.
这是我的代码:
public class ButterknifeCustomBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return arrayListNames.size();
}
@Override
public Name getItem(int iPosition) {
return arrayListNames.get(iPosition);
}
@Override
public long getItemId(int iID) {
return 0;
}
LayoutInflater inflater;
ArrayList<Name> arrayListNames = new ArrayList<Name>();
static Context context;
Activity activity;
public ButterknifeCustomBaseAdapter(Context context, ArrayList<Name> names, Activity activity) {
arrayListNames = names;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
static class ViewHolder implements View.OnClickListener {
@InjectView(R.id.textViewFullName) TextView textViewFullName;
@InjectView(R.id.imageButtonDeleteName) TextView imageButtonDeleteName; …Run Code Online (Sandbox Code Playgroud) 我正在使用下面的代码生成带有一些注释的简单图表:
require(ggplot2); data(mtcars)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
annotate("text", x = 4, y = 25, label = "This should be bold\nand this not",
colour = "red") +
geom_vline(xintercept = 3.2, colour = "red")
Run Code Online (Sandbox Code Playgroud)
在该图表上,我想将粗体字体应用于文本注释中短语的第一部分:
这应该是大胆的
但我希望文本的其余部分在字体和样式方面保持不变.
鉴于以下课程:
using System.ComponentModel.DataAnnotations;
public class Book{
public Contact PrimaryContact{get; set;}
public Contact SecondaryContact{get; set;}
[Required(ErrorMessage="Book name is required")]
public string Name{get; set;}
}
public class Contact{
[Required(ErrorMessage="Name is required")]
public string Name{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
有没有干净的方式我可以给每个实例的不同错误信息Contact中Book使用DataAnnotations?例如,如果PrimaryContact实例中缺少名称,则错误将显示为"需要主要联系人姓名".
我目前的解决方案是创建一个验证服务,检查模型状态是否存在字段错误,然后删除所述错误并使用我喜欢的特定语言将其添加回来.
我想根据以下链接中的示例进行一些注释处理:http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm.
但是,我想在我的Android项目中实现这个,似乎我不能使用android平台的包.我是否需要添加外部罐子或者是否有我遗漏的东西?
谢谢.
我正在使用JAX-RS我的网络服务.我有共同的功能,并希望使用继承.我提供简单的CRUD操作.我已经定义了这样的接口:
public interface ICRUD {
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("create")
public String createREST(String transferObject);
@GET
@Consumes("application/json")
@Produces("application/json")
@Path("retrieve/{id}")
public String retrieveREST(@PathParam("id") String id);
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("update")
public void updateREST(@Suspended final AsyncResponse asyncResponse,
final String transferObject) ;
@DELETE
@Consumes("application/json")
@Produces("application/json")
@Path("delete/{id}")
public String deleteREST(@PathParam("id") String id);
}
Run Code Online (Sandbox Code Playgroud)
我有一个实现此接口的抽象类:
public abstract class BaseREST implements ICRUD{
private final ExecutorService executorService = Executors.newCachedThreadPool();
@Override
public String createREST(String transferObject) {
return create(transferObject).toJson();
}
@Override
public String retreiveREST(@PathParam("id") String id) {
return retreive(id).toJson();
} …Run Code Online (Sandbox Code Playgroud) 我想要ETag支持.为此目的,有一个ShallowEtagHeaderFilter完成所有工作.如何添加它而不在我的声明web.xml(实际上不存在,因为我到目前为止没有它的某种方式得到它)?
PS我使用Spring Boot 1.1.4
PPS这是一个完整的解决方案
package cuenation.api;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.DispatcherType;
import java.util.EnumSet;
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean shallowEtagHeaderFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ShallowEtagHeaderFilter());
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
registration.addUrlPatterns("/cue-categories");
return registration;
}
}
Run Code Online (Sandbox Code Playgroud) annotations ×10
java ×5
android ×2
spring ×2
asp.net-mvc ×1
baseadapter ×1
butterknife ×1
c# ×1
charts ×1
ggplot2 ×1
hibernate ×1
inheritance ×1
java-5 ×1
jax-rs ×1
many-to-many ×1
metadata ×1
r ×1
rest ×1
spring-boot ×1
sql ×1
text ×1
validation ×1
web.xml ×1