小编dan*_*nik的帖子

从Spring MVC控制器中的Security Context获取UserDetails对象

我正在使用Spring Security 3和Spring MVC 3.05.

我想打印当前登录用户的用户名,如何在Controller中获取UserDetails?

@RequestMapping(value="/index.html", method=RequestMethod.GET)
    public ModelAndView indexView(){
         UserDetails user = ?
                mv.addObject("username", user.getUsername());
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }   
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security

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

日期的休眠标准

在oracle我有格式的日期

2011年4月17日19:20:23.707000000

我想检索17-04-2011的所有订单.

 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
Date date = formatter.parse(myDate);
    Criteria criteria = 
                session.createCriteria(Order.class);
Criterion restrictDate = Restrictions.like("orderDate",date); 
Run Code Online (Sandbox Code Playgroud)

但它给我带来空洞的结果:

java oracle hibernate criteria java-ee

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

Java 6中的URL解码

我看到它java.net.URLDecoder.decode(String)在6 中被弃用了.

我有以下字符串:

String url ="http://172.20.4.60/jsfweb/cat/%D7%9C%D7%97%D7%9E%D7%99%D7%9D_%D7%A8%D7%92%D7%99%D7%9C%D7%99%D7%9"
Run Code Online (Sandbox Code Playgroud)

我应该如何在Java 6中解码它?

java

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

使用复合主键和注释映射ManyToMany:

我正在尝试使用复合主键在学生和教学课程之间创建许多非常多的关系:

我的课程:

@Entity
@Table(name="Student_mtm_cId")
public class Student {

    private String id;
    private Set<StudentTClass> teachingClasses = new HashSet<StudentTClass>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.student")
    public Set<StudentTClass> getTeachingClasses() {
        return teachingClasses;
    }
    public void setTeachingClasses(Set<StudentTClass> teachingClasses) {
        this.teachingClasses = teachingClasses;
    }

    public void addStudentToClass(TeachingClass teachingClass){
        StudentTClass studentTClass = new StudentTClass();
        studentTClass.setStudent(this);
        studentTClass.setTeachingClass(teachingClass);
        teachingClasses.add(studentTClass);
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
     @Column(name = "student_id", nullable = false)
    public String getId() {
        return id;
    } …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate java-ee

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

Spring标准Bean注入与自动装配

据我所知,当使用依赖注入时,所有bean都在Start上初始化.

<bean id="userPreferences" class="com.foo.UserPreferences">
  </bean>
  <!-- a singleton-scoped bean injected to the above bean -->
  <bean id="userService" class="com.foo.SimpleUserService">

      <!-- a reference to the userPreferences bean -->
      <property name="userPreferences" ref="userPreferences"/>
  </bean>
Run Code Online (Sandbox Code Playgroud)

并且上面的配置意味着在应用程序启动时创建的userService和userPreferences.这是对的吗?

使用自动装配和使用时 <context:component-scan>

public class SimpleUserService{

@Autowired
UserPreferences userPreferences;

//omitted
} 
Run Code Online (Sandbox Code Playgroud)

1)是否在Application init上创建了userPreference?

2)autowire注入的bean的默认范围是什么?我们如何更改它?

3)如何影响豆类创造和豆类注射?

希望我清楚自己.

spring spring-mvc

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

JNDI路径Tomcat与Jboss

我有DataSource,它在context.xml中的T​​omcat 6上配置为MyDataSource.我正在通过以下方式获取它:

      DataSource dataSource;
            try {
                dataSource = (DataSource) new InitialContext().lookup("java:comp/env/MyDataSource");
            } catch (NamingException e) {
                throw new DaoConfigurationException(
                    "DataSource '" + url + "' is missing in JNDI.", e);
            }
Run Code Online (Sandbox Code Playgroud)

一切正常.现在我将此代码导出到Jboss AP 6.我将我的dataSource及其连接池配置为local-tx dataSource,名称相同.

当我执行上面的代码时,我收到了NamingException异常.经过一番调查后,我发现在Jboss下调用我的DataSource的正确方法是

 dataSource = (DataSource) new InitialContext().lookup("java:/MyDataSource");
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释一下我为什么要在Jboss下的JNDI路径中省略"comp/env"?

java jboss tomcat jndi jdbc

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

如何在Spring MVC中的Post/Redirect/Get模式请求之间传递数据?

我有一个表单,使用POST signin.html提交用户名和姓

 @RequestMapping(value="/signin.html",method = RequestMethod.POST)
 public ModelAndView submit(@Valid User user){
    ModelAndView mv = new ModelAndView("redirect:signin.html"); 
    //Business logic with user account
    return mv;
 } 
Run Code Online (Sandbox Code Playgroud)

为了解决双提交问题,我使用GET请求重定向到相同的映射.

 @RequestMapping(value="/signin.html",method = RequestMethod.GET)
 public ModelAndView submitPRG(){
     ModelAndView mv = new ModelAndView("submitted");
     mv.addObject("message", "Submitted Correctly");
     return mv;
 }
Run Code Online (Sandbox Code Playgroud)

这样我解决了双提交问题.

我有几个问题:

1)我如何知道/signin.html上的GET请求来自重定向并且用户未在浏览器中请求?我只想关闭用户浏览http://server/signin.html并获得"正确提交"消息的选项.我知道我可以添加类似/signin.html?submitted=true的内容,但我想让它更干净.

2)有没有办法将ModelAndView对象传递submit()submitPRG()

或者在这种情况下,还有其他方法可以使用PRG吗?

spring spring-mvc parameter-passing post-redirect-get

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

Tomcat 7上的Servlet 3异步任务

我正在尝试使用基于其异步支持的Servlet 3.0和Comet模式实现简单聊天.

我受到这篇文章的启发:http: //www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page = 3

我的servlet看起来像这样.

@WebServlet(name="chatServlet", urlPatterns={"/ChatServlet"}, asyncSupported=true)
public class ChatServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         AsyncContext aCtx = request.startAsync(request, response); 
         ServletContext appScope = request.getServletContext();    
         List<AsyncContext> watchers = (List<AsyncContext>) appScope.getAttribute("watchers");
         watchers.add(aCtx); //register the watcher
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          AsyncContext aCtx = request.startAsync(request, response); 
          ServletContext appScope = request.getServletContext(); 
          Queue<String> messages = (Queue<String>)appScope.getAttribute("messages");
          messages.add(someMessage);
    }
} 
Run Code Online (Sandbox Code Playgroud)

现在我的听众看起来像这样:

@WebListener
public class ChatPushService implements ServletContextListener …
Run Code Online (Sandbox Code Playgroud)

java servlets comet java-ee tomcat7

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

播放2 - 无法在响应中返回Json对象

我正在尝试使用Play 2.1.3做一些RESTFull Web服务POC

我有以下课程:

case class Student(id: Long,firstName: String,lastName: String) 
Run Code Online (Sandbox Code Playgroud)

现在我想创建RESTfull URI,它将获得Json序列化的学生POJO并返回相同的POJO作为响应.

implicit val studentReads = Json.reads[Student]
implicit val studentWrites = Json.writes[Student]


def updateStudent = Action(parse.json){
  request=>request.body.validate[Student].map{
    case xs=>Ok(xs)}.recoverTotal{
      e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
    }
  } 
Run Code Online (Sandbox Code Playgroud)

但我得到编译错误 -

Cannot write an instance of entities.Student to HTTP response. Try to define a 
     Writeable[entities.Student]
Run Code Online (Sandbox Code Playgroud)

我刚刚提供Writes[A]了隐式变量.

我还缺少什么?

scala playframework playframework-2.0

10
推荐指数
1
解决办法
7661
查看次数

使用Hibernate映射数组

你可以帮我映射一下Hbernate吗?

public class MyClass{
    private Long id;
    private String name;
    private int[] values;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在使用PostgreSQL,表格的列类型是整数[]我的数组应该如何映射?

java postgresql hibernate java-ee

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