初学spring遇到的问题

rendajuan0711 2009-07-19 05:13:16
刚刚学习spring遇到问题。
(一)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--
- Application context definition for "example" DispatcherServlet.
- Heavily depended on web infrastructure, naturally.
-->
<beans>
<bean id="fru" name="com.hyjk.Apple"></bean>
</beans>
(二)测试页面如下 :
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="com.hyjk.*" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试Spring页面</title>
</head>
<body>
<%
ApplicationContext context=null;
context=new ClassPathXmlApplicationContext("applicationContext.xml");
Fruit f=(Fruit)context.getBean("fru");
f.eat();
%>

</body>
</html>
(三)
package com.hyjk;

public class Apple implements Fruit {

@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("苹果");
}

}
在IE中输入 :http://localhost:8080/TestProject/meter/test.jsp
错误如下:
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
Only a type can be imported. org.springframework.context.ApplicationContext resolves to a package

An error occurred at line: 8 in the generated java file
Only a type can be imported. org.springframework.context.support.ClassPathXmlApplicationContext resolves to a package

An error occurred at line: 14 in the jsp file: /meter/test.jsp
ApplicationContext cannot be resolved to a type
11: </head>
12: <body>
13: <%
14: ApplicationContext context=null;
15: context=new ClassPathXmlApplicationContext("applicationContext.xml");
16: Fruit f=(Fruit)context.getBean("fru");
17: f.eat();


An error occurred at line: 15 in the jsp file: /meter/test.jsp
ClassPathXmlApplicationContext cannot be resolved to a type
12: <body>
13: <%
14: ApplicationContext context=null;
15: context=new ClassPathXmlApplicationContext("applicationContext.xml");
16: Fruit f=(Fruit)context.getBean("fru");
17: f.eat();
18: %>
请问该如何修改?
...全文
468 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rendajuan0711 2009-07-20
  • 打赏
  • 举报
回复
我照三楼的做了,还是同样的错误
rendajuan0711 2009-07-20
  • 打赏
  • 举报
回复
多谢大家的帮忙,我不知道应该怎么样动手去学习spring,看了以前的视频,是介绍这么学的,所以。。。。。哪位能给我 一些学习spring的建议呢,多谢!
TD.Lemon 1 9 0 0 2009-07-19
  • 打赏
  • 举报
回复
在页面 写这些? 为什么不 用J2SE来测试呢? 。。。。。。。。。。。楼上说的 你可以试试,JSP看着乱~~~~~~~~~~~~
cjdxhc 2009-07-19
  • 打赏
  • 举报
回复
ApplicationContext context=new ClassPathXmlApplicationContext("/applicationContext.xml");

不建议在页面上用小脚本的形式这样使用!既然都学Spring了,就应该明显分层开发!页面一般是用来显示的view层!
悠云guo 2009-07-19
  • 打赏
  • 举报
回复
JSP页面上加入
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>


不过,不建议如此使用Spring
rendajuan0711 2009-07-19
  • 打赏
  • 举报
回复
没有理我

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧