最简单的springmvc

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   
 8   <context-param>
 9       <param-name>contextConfigLocation</param-name>
10       <param-value>classpath:applicationContext.xml</param-value>
11   </context-param>
12   
13   <listener>
14       <listener-class>
15           org.springframework.web.context.ContextLoaderListener
16       </listener-class>
17   </listener>
18   
19   <servlet>
20       <servlet-name>springmvc</servlet-name>
21       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
22       <load-on-startup>1</load-on-startup>
23   </servlet>
24   
25   <servlet-mapping>
26       <servlet-name>springmvc</servlet-name>
27       <url-pattern>/</url-pattern>
28   </servlet-mapping>
29   
30   
31 </web-app>

springmvc-servlet.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8        http://www.springframework.org/schema/context 
 9        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10     
11     <context:component-scan base-package="com.controller"/>
12     
13     <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
14     <bean
15         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
16         p:viewClass="org.springframework.web.servlet.view.JstlView" 
17         p:prefix="/WEB-INF/views/"
18         p:suffix=".jsp" />
19 
20 </beans>

applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10        http://www.springframework.org/schema/context 
11        http://www.springframework.org/schema/context/spring-context-3.0.xsd
12        http://www.springframework.org/schema/tx 
13        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14        http://www.springframework.org/schema/aop
15        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
16    
17 </beans>

login.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 <form action="login" method="post">
 9 姓名:<input type="text" name="username"/><br>
10 密码:<input type="password" name="password"/><br>
11 <input type="submit" value="登录">
12 </form>
13 </body>
14 </html>

main.jsp:

1 <body>
2 welcome,${user.username}...
3 </body>

User.java:

 1 public class User {
 2     private String username;
 3     private String password;
 4     public String getUsername() {
 5         return username;
 6     }
 7     public void setUsername(String username) {
 8         this.username = username;
 9     }
10     public String getPassword() {
11         return password;
12     }
13     public void setPassword(String password) {
14         this.password = password;
15     }
16     
17 }

IndexController.java:

 1 import org.springframework.stereotype.Controller;
 2 import org.springframework.web.bind.annotation.RequestMapping;
 3 
 4 @Controller
 5 public class IndexController {
 6 
 7     @RequestMapping("/index")
 8     public String forward(){
 9         return "login";
10     }
11 }

LoginController:

 1 import javax.servlet.http.HttpServletRequest;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 import com.domain.User;
 7 
 8 @Controller
 9 
10 public class LoginController {
11     
12     @RequestMapping("/login")
13     public String login(HttpServletRequest request,User user){
14         if(user.getUsername().equals("scott")&& user.getPassword().equals("tiger")){
15             request.getSession().setAttribute("user",user);
16             return "main";
17         }else{
18             return "login";
19         }
20     }
21 }

 

 

posted on 2014-04-09 17:00  confirmBname  阅读(159)  评论(0编辑  收藏  举报

导航