spring-helloword

1.新建Dynamic Web Project 项目->why

2下载springmvc jar,并拷贝到WebContent/WEB-INF/lib

3.配置web.xml如下(位于\WEB-INF):

   

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 ok
11 </body>
12 </html>

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3  <servlet>
 4   <servlet-name>springMVC</servlet-name>
 5   <!-- 配置处理请求的核心类 -->
 6   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 7   <load-on-startup>1</load-on-startup>
 8 </servlet>
 9 
10 <servlet-mapping>
11     <servlet-name>springMVC</servlet-name>
12     <!-- 拦截spring请求 -->
13     <url-pattern>*.spring</url-pattern>
14 </servlet-mapping>
15 
16   <welcome-file-list>
17     <welcome-file>index.jsp</welcome-file>
18   </welcome-file-list>
19 </web-app>

4.配置springMVC-servlet.xml(也位于WEB-INF下)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 4 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 7 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 8 <!-- 扫描控制器包,带@Controller的是控制java文件,处理request和response请求-->
 9 <context:component-scan base-package="controller" />
10 </beans>

5.相关jsp 文件,一个登陆login.jsp,登陆名如果为gao调到ok.jsp,否则为no.jsp

login.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <form action="login.spring" method="post">
   username:
   <input type="text" name="username">
   <br/>
   password:
   <input type="text" name="password">
   <br/>
   <input type="submit" value="submit">
   <br/>
   </form>

</body>
</html>

 

6.核心控制类位于src下 controller.Login

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller//代表是Java类的Controller控制层
public class Login {
    @RequestMapping("/login")//指定可以访问本控制层的url
    public String login1(@RequestParam("username")String username,
            @RequestParam("password")String password,Model model) {
        if(username.equals("gao"))
            return "ok.jsp";
        return "no.jsp";
        
    }

}

运行展示:

 

posted on 2017-07-16 15:51  笑傲江湖未平生  阅读(152)  评论(0)    收藏  举报