安装Tomcat
-
下载一个tomcat,解压
-
Tomcat的用户帐号信息都保存在tomcat-users.xml的文件中,编辑该文件,添加用户:
<user username="用户名" password="密码" roles="admin,manager"/>
-
安装好jdk后,直接到tomcat/bin/startup.sh 启动
-
localhost:8080查看是否启动
-
实时察看tomcat日志:tail -f ./tomcat/logs/catalina.out
下载Eclipse for J2EE
-
安装svn插件:http://subclipse.tigris.org/update_1.6.x ,这个无所谓,随手记录一下
-
配置tomcat:打开window->preference 点击server,选择Run Invironment,Add 一个tomcat,相关地址就是上面的tomcat解压目录
-
新建一个动态web项目,new ->file->other 一个jsp 文件,body里随便写点内容,在show view中选择servers,然后new一个server,就是上面配置的Run Invironment中的tomcat名字,然后启动这个server
-
测试,地址为:localhost:8080/项目名/test.jsp
参考文档:
http://wenku.baidu.com/view/2f9b7e046c85ec3a87c2c5b1.html
http://www.iteye.com/topic/982182
Spring mvc 入门
web 接口一般有jsp或者html表单交互式和httpGet,httpPost直接请求式,也就是类似.do访问的
这两种配置有点差别,下面分别介绍一下:
准备工作:在上面的环境搭建好后,新建一个动态web工程
请求地址:http://localhost:8080/jspTest/hello.do
返回:hello.jsp
首先配置web.xml
在WEB-INF/web.xml,内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id=
"WebApp_ID"
version=
"2.5"
>
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-
class
>org.springframework.web.servlet.DispatcherServlet</servlet-
class
>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.
do
</url-pattern>
</servlet-mapping>
</web-app>
|
主要是配置拦截什么类型的请求,以及由哪个servlet去处理
再看看WEB-INF/spring-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-
package
=
"com.test"
/>
<bean id=
"viewResolver"
class
=
"org.springframework.web.servlet.view.UrlBasedViewResolver"
>
<property name=
"viewClass"
value=
"org.springframework.web.servlet.view.JstlView"
/>
<property name=
"prefix"
value=
"/WEB-INF/jsp/"
/>
<property name=
"suffix"
value=
".jsp"
/>
</bean>
</beans>
|
返回不通过jsp的话,上面那段<bean></bean>就可以不要
再看看controller,使用注解方式(也就是类似java doc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package
com.test;
import
java.io.IOException;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
net.sf.json.JSONObject;
import
org.springframework.stereotype.Controller;
import
org.springframework.ui.Model;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.servlet.ModelAndView;
@Controller
public
class
HelloWorldController {
@RequestMapping(
"/hello.do"
)
public
ModelAndView helloWorld() {
System.out.println(
"HelloWorldController"
);
String
message =
"Hello World, Spring 3.0!"
;
return
new
ModelAndView(
"hello"
,
"message"
, message);
}
@RequestMapping(value=
"/welcome.do"
,method=RequestMethod.GET)
public
void
welcome(HttpServletRequest request,HttpServletResponse response) {
System.out.println(
"welcome"
);
String
name=request.getParameter(
"userName"
);
String
pwd=request.getParameter(
"pwd"
);
String
message =
null
;
try
{
JSONObject content=
new
JSONObject();
content.put(
"name"
, name);
content.put(
"pwd"
, pwd);
System.out.println(content.toString());
message=content.toString();
}
catch
(Exception e) {
e.printStackTrace();
}
try
{
response.getOutputStream().write(message.getBytes());
}
catch
(IOException e) {
e.printStackTrace();
}
// return new ModelAndView("json","message",message);
}
@RequestMapping(value =
"/hello2.do"
)
public
String
hello2(
int
id,Model model){
System.out.println(
"hello2 action:"
+id);
model.addAttribute(
"name"
,
"huangjie"
);
//这个只有值没有键的情况下,使用Object的类型作为key,String-->string
model.addAttribute(
"ok"
);
return
"hello"
;
}
//得到request,response,session等,只要在方法形参中声明参数即可
@RequestMapping(value =
"/hello3.do"
)
public
String
hello3(HttpServletRequest request){
String
id = request.getParameter(
"id"
);
System.out.println(
"hello3 action:"
+id);
return
"hello"
;
}
}
|
把整个代码上传到github吧,我找相应的jar包就找了半天,现在还没搞明白原始下载地址在哪下载,spring上面也没有download
github:https://github.com/ahong222/spring_test.git
打包:导出war到 tomcat/webapps/目录下即可
附:如果希望在路径中改变,如何获取改变的那个路径方法呢?
@RequestMapping(value = "/a/{name}/verify.do", method = RequestMethod.GET)
public void verify(@PathVariable("name") String name,HttpServletRequest request,
HttpServletResponse response) {
}
以上访问方法为***/a/name1/verify.do ,这样服务端就能获取到“name1”了
(2013-12-30 写于百度空间)