标签: annotations

使用Spring DispatcherServlet的自定义404

我已经设置了如下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)

spring annotations web.xml http-status-code-404

22
推荐指数
1
解决办法
3万
查看次数

为什么Java 5中不推荐使用java.io.Serializable?

在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 无法找到)

java serialization annotations metadata java-5

22
推荐指数
2
解决办法
5368
查看次数

Java Annotations值以动态方式提供

我想提供注释与一些方法生成的一些值.

到目前为止我试过这个:

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_NAMEstatic final,它抱怨说

注释属性的值MyInterface.aString必须是常量表达式

那么如何实现呢?

java annotations

22
推荐指数
3
解决办法
3万
查看次数

Hibernate多对多级联删除

我在我的数据库3个表:Students,CoursesStudents_Courses

学生可以有多门课程,课程可以有多个学生.之间存在许多一对多的关系StudentsCourses.

我有3个案例用于我的项目,课程已添加到我的Courses表格中.

  • (a)当我添加一个用户时,它被保存得很好,
  • (b)当我为学生添加课程时,它会创建新的行User_Courses- 再次,预期的行为.
  • (三)当我试图删除学生,则在删除适当的记录StudentsStudents_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)

java sql annotations many-to-many hibernate

22
推荐指数
2
解决办法
4万
查看次数

在自定义BaseAdapter子类中使用Butter Knives导致"无法注入视图"错误

我正在尝试使用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)

java android annotations baseadapter butterknife

22
推荐指数
2
解决办法
2万
查看次数

在注释中使用不同的字体样式(ggplot2)

我正在使用下面的代码生成带有一些注释的简单图表:

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)

简单的情节

在该图表上,我想将粗体字体应用于文本注释中短语的第一部分:

这应该是大胆的

但我希望文本的其余部分在字体和样式方面保持不变.

charts text annotations r ggplot2

22
推荐指数
3
解决办法
3万
查看次数

使用数据注释进行模型验证的错误消息

鉴于以下课程:

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)

有没有干净的方式我可以给每个实例的不同错误信息ContactBook使用DataAnnotations?例如,如果PrimaryContact实例中缺少名称,则错误将显示为"需要主要联系人姓名".

我目前的解决方案是创建一个验证服务,检查模型状态是否存在字段错误,然后删除所述错误并使用我喜欢的特定语言将其添加回来.

c# validation asp.net-mvc annotations data-annotations

21
推荐指数
2
解决办法
6万
查看次数

Android javax.annotation.processing包丢失

我想根据以下链接中的示例进行一些注释处理:http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm.

但是,我想在我的Android项目中实现这个,似乎我不能使用android平台的包.我是否需要添加外部罐子或者是否有我遗漏的东西?

谢谢.

android annotations annotation-processing

21
推荐指数
4
解决办法
2万
查看次数

使用JAX-RS继承

我正在使用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)

java rest inheritance annotations jax-rs

21
推荐指数
1
解决办法
1万
查看次数

在Spring Boot应用程序中添加Servlet过滤器

我想要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)

spring annotations servlet-filters spring-boot

21
推荐指数
1
解决办法
6万
查看次数