小编blo*_*824的帖子

Spring Jndi配置,Server.xml

我在使用Spring为JNDI设置配置时遇到问题.我检查了其他帖子,但无法解决我的问题.我使用Tomcat 6作为我的容器.根据我的理解,我需要在服务器上设置资源.所以在我的server.xml文件中我有这个:

<GlobalNamingResources>
    <Resource auth="Container" driverClassName="org.postgresql.Driver"
            maxActive="100" maxIdle="5" maxWait="10000"
            minEvictableIdleTimeMillis="60000" name="jdbc/myTomcatPool"
            password="password" testOnBorrow="true" testWhileIdle="true"
            timeBetweenEvictionRunsMillis="10000" type="javax.sql.DataSource"
            url="jdbc:postgresql://localhost:5432/postgis" username="postgres"
            validationQuery="SELECT 1"/>
</GlobalNamingResources>
Run Code Online (Sandbox Code Playgroud)

我在我的spring-context.xml(在类路径上)有以下内容:

<jee:jndi-lookup id="geoCodeData" jndi-name="java:comp/env/jdbc/myTomcatPool" />

<bean id="geoCodeService" class="com.sample.SampleImpl">
    <property name="dataSource" ref="geoCodeData"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

然后我在文件中有这个META-INF/context.xml:

<Context path="/myApp" reloadable="true" cacheMaxSize="51200"
        cacheObjectMaxSize="2560">
    <ResourceLink global="jdbc/myTomcatPool" name="jdbc/myTomcatPool"
            type="javax.sql.DataSource"/>
</Context>
Run Code Online (Sandbox Code Playgroud)

我的服务器启动时没有错误.

当我尝试运行以下测试时(在我添加JNDI代码之前有效):

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx =
            new ClassPathXmlApplicationContext("spring-context.xml");
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

线程"main"中的异常org.springframework.beans.factory.BeanCreationException:创建名为'geoCodeData'的bean时出错:init方法的调用失败;

嵌套异常是javax.naming.NoInitialContextException:需要在环境或系统属性或applet参数或应用程序资源文件中指定类名:java.naming.factory.initial

我的配置是错误的还是我尝试运行测试的方式不正确?

java configuration spring jndi

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

嵌套对象最佳实践

引用嵌套对象的最佳做法是什么?

说我有以下内容:

class Outer {
 private InnerA innerA;
 //getters and setters
}

class InnerA {
  private InnerB innerB;
  //getters and setters
}

class InnerB {
  private String someString;
  //getters and setters
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器或服务类中,我需要检查InnerB类的someString String变量,以确保它不为null或不为空,所以我这样做:

if (getOuter().getInnerA().getInnerB().getSomeString() != null && !getOuter().getInnerA().getInnerB().getSomeString().equalsIgnoreCase("") {
  //do something
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这看起来很乱,如果嵌套对象本身为null,则可能会出现问题.

我是否在父对象中为子对象检查null创建getter和setter?只是想知道最佳实践是什么,如果有什么和/或你的一些人在你的代码中做了什么?

java nested reference object

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

需要有关带名称空间的xPath表达式的帮助

我需要一些xpath表达式的帮助.

我有以下xml文档:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns="http://schemas.test.com/search/local/ws/rest/v1">
    <nodeA>text</nodeA>
    <nodeB>
        <nodeb1>
            <nodeb1a>
                <nodeTest>hello</nodeTest>
            </nodeb1a>
        </nodeb1>
    </nodeB>
</Response>
Run Code Online (Sandbox Code Playgroud)

我正在使用Springs rest模板来检索数据.

这是我使用xpath表达式获取节点值的地方:

xpathTemplate.evaluate("base:*", xmlSource, new NodeMapper() {
        public Object mapNode(Node node, int i) throws DOMException {
            Element response = (Element) node;
            System.out.println("hi: " + response.getTextContent());
Run Code Online (Sandbox Code Playgroud)

我将base添加到我的命名空间映射中:

HashMap<String, String> nameSpaces = new HashMap<String, String>();
nameSpaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nameSpaces.put("xsd", "http://www.w3.org/2001/XMLSchema");
nameSpaces.put("base", "http://schemas.test.com/search/local/ws/rest/v1");
((Jaxp13XPathTemplate) xpathTemplate).setNamespaces(nameSpaces);
Run Code Online (Sandbox Code Playgroud)

使用表达式base:*为我提供了Response节点,我可以使用.getChildNodes向下钻取到子节点,并为每个子节点创建节点列表.

我想直接用xpath表达式获取hello值但是我无法使用除了base之外的任何东西:*.有人可以用我的表达来帮助我吗?

谢谢

java spring xpath expression

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

Spring XML View Resolver Conifguration

我试图使用spring-mvc将一些模型数据输出到pdf.它不起作用,我想知道是否有人可以提供一些建议.

我有一个spring-servlet.xml文件,其中包含以下内容:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="order" value="2"/>
    <property name="location">
        <value>/WEB-INF/spring-pdf-views.xml</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

在spring-pdf-views.xml文件中,我有:

<bean id="MyPDF" class="com.example.MyPDFView"/>
Run Code Online (Sandbox Code Playgroud)

这是我的MyPDFView类:

public class MyPDFView extends AbstractPdfView {

    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        @SuppressWarnings("unchecked")
        Map<String, String> data = (Map<String, String>) model.get("modelData");

        Table table = new Table(2);
        table.addCell("Date");
        table.addCell("Name");
        table.addCell(data.get("modelData.dateValue"));
        table.addCell(data.get("modelData.nameValue"));

        document.add(table);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

最后在我的控制器中我有:

@RequestMapping(value="/pdfInformation", method=RequestMethod.POST)
public ModelAndView …
Run Code Online (Sandbox Code Playgroud)

java xml spring view resolver

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

Spring Security 3.0重定向到超时的页面

我正在使用Spring Security 3.0.6,我希望能够执行以下操作:

如果用户在页面上并且发生会话超时,则用户将进入登录页面并在有效登录时重定向回到发生超时的页面.

我目前在security.xml文件中有以下内容.

<http auto-config="true" use-expressions="true">
    <form-login
        login-page="/login" 
        default-target-url="/main" 
        always-use-default-target="false"
        authentication-failure-url="/login.html?error=true"
        authentication-success-handler-ref="authenticationSuccessHandler" />
    <remember-me/>
    <logout logout-success-url="/login" />
</http>
Run Code Online (Sandbox Code Playgroud)

这是我的身份验证类:

public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {

    String url = "";

    HttpSession session = request.getSession(false);
    if (session != null) {
        SavedRequest savedRequest = (SavedRequest) session.getAttribute(WebAttributes.SAVED_REQUEST);
        if (savedRequest != null) {
            url = savedRequest.getRedirectUrl();
        }
    }

    System.out.println("url: "+ url);

    if (url == "") {
        response.sendRedirect(request.getContextPath()+"/main");
    } else {
        response.sendRedirect(url);
    } …
Run Code Online (Sandbox Code Playgroud)

java security redirect spring

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

本地安装弹簧温室的问题

我正在尝试安装spring的温室项目,我遇到了一些错误.

这是我正在努力的网站: 温室

我能够下载eclipse git插件并安装项目,但它没有构建.

我的日志是

4/7/11 7:54:44 AM MST: Maven Builder: FULL_BUILD 
4/7/11 7:54:44 AM MST: [WARN] 
4/7/11 7:54:44 AM MST: [WARN] Some problems were encountered while building the effective model for com.springsource:greenhouse:war:1.0.0-BUILD-SNAPSHOT
4/7/11 7:54:44 AM MST: [WARN] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 418, column 12
4/7/11 7:54:44 AM MST: [WARN] 'distributionManagement.snapshotRepository.id' must not be 'local', this identifier is reserved for the local repository, using it for other repositories will corrupt your repository metadata. @ line 575, …
Run Code Online (Sandbox Code Playgroud)

java spring

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

帮助在春季捕获会话超时

我有一个简单的Spring 3 MVC应用程序.我正在使用sessionAttribute,一切正常,除非我让页面停留30分钟或更长时间.然后我得到一个

org.springframework.web.HttpSessionRequiredException

告诉我在会话中找不到我的对象.

我想我需要在会话超时发生时以某种方式重定向回同一页面.我不确定如何正确使用弹簧.

无需登录,我已经在检查对象是否为空.

任何建议,将不胜感激.

谢谢

java session spring timeout

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

将Tomcat库添加到Maven

我正在Maven进行第一次破解并遇到了问题.我们有一个部署在Tomcat 6上的应用程序.我们在tomcat的lib文件夹中添加了几个jar文件.然后在我们的构建路径中添加这个tomcat库.

如何将tomcat库添加到maven?这是一个不好的方法吗?还有其他选择吗?

提前致谢

java tomcat maven

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

带有集合的Hibernate Annotations

我正在尝试使用hibernate注释实现我的模型.我有3个班级,图像,人物和标签.标签是由4个字段组成的表,id,personId,imageId和createdDate.Person有字段名,id,birthdate等.我的图像类定义如下:

@Entity
@Table(name="Image")
public class Image {
    private Integer imageId;
    private  Set<Person> persons = new HashSet<Person>();

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    public Integer getImageId() {
        return imageId;
    }

    public void setImageId(Integer imageId) {
        this.imageId = imageId;
    }

    @ManyToMany
    @JoinTable(name="Tags",
                joinColumns = {@JoinColumn(name="imageId", nullable=false)},
                inverseJoinColumns = {@JoinColumn(name="personId", nullable=false)})
    public Set<Person> getPersons() {
        return persons;
    }

    public void setPersons(Set<Person> persons) {
        this.persons = persons;
    }
Run Code Online (Sandbox Code Playgroud)

如果我删除了getPersons()方法上的注释,我可以使用这些类并添加和删除记录.我想用图像获取所有标签,我正在尝试使用一组.我一直收到以下错误:

org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.exmaple.persons, no session or …
Run Code Online (Sandbox Code Playgroud)

java collections configuration annotations hibernate

0
推荐指数
1
解决办法
980
查看次数