Java Spring 入门

安装Tomcat

  1. 下载一个tomcat,解压

  2. Tomcat的用户帐号信息都保存在tomcat-users.xml的文件中,编辑该文件,添加用户:

    <user username="用户名" password="密码" roles="admin,manager"/>
  3. 安装好jdk后,直接到tomcat/bin/startup.sh 启动

  4. localhost:8080查看是否启动

  5. 实时察看tomcat日志:tail -f  ./tomcat/logs/catalina.out



下载Eclipse for J2EE

  1. 安装svn插件:http://subclipse.tigris.org/update_1.6.x ,这个无所谓,随手记录一下

  2. 配置tomcat:打开window->preference 点击server,选择Run Invironment,Add 一个tomcat,相关地址就是上面的tomcat解压目录

  3. 新建一个动态web项目,new ->file->other 一个jsp 文件,body里随便写点内容,在show view中选择servers,然后new一个server,就是上面配置的Run Invironment中的tomcat名字,然后启动这个server

  4. 测试,地址为: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" ?>
     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" ?>
   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 写于百度空间)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值