读书人

spring3mvc 容易配置

发布时间: 2012-09-02 21:00:34 作者: rapoo

spring3mvc 简单配置
使用spring3的注解吧,好简单
web.xml

   <?xml version="1.0" encoding="UTF-8"?>   <web-app version="2.5"       xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">                   <context-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/springmvc-servlet.xml</param-value>        </context-param>        <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>        </listener>                                <!--Character Encoding Convert-->        <filter>            <filter-name>encodingFilter</filter-name>            <filter-class> org.springframework.web.filter.CharacterEncodingFilter             </filter-class>            <init-param>                <param-name>encoding</param-name>                <param-value>utf-8</param-value>            </init-param>        </filter>        <filter-mapping>            <filter-name>encodingFilter</filter-name>            <url-pattern>*.do</url-pattern>        </filter-mapping>        <!-- Spring Security Filter-->       <filter>                           <servlet>            <servlet-name>springmvc</servlet-name>            <servlet-class> org.springframework.web.servlet.DispatcherServlet             </servlet-class>         </servlet>        <servlet-mapping>            <servlet-name>springmvc</servlet-name>            <url-pattern>*.do</url-pattern>        </servlet-mapping>                        <welcome-file-list>       <welcome-file>index.jsp</welcome-file>     </welcome-file-list>   </web-app>  


springmv-servlet.xml

   view plaincopy to clipboardprint?<?xml version="1.0" encoding="UTF-8"?>   <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"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:jdbc="http://www.springframework.org/schema/jdbc"       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                http://www.springframework.org/schema/tx                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                http://www.springframework.org/schema/jdbc                http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd               http://www.springframework.org/schema/mvc               http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">       <bean           />       <bean           />       <!-- 自动搜索@Controller标注的类 -->       <context:component-scan base-package="com.zuxiang.web" />       <!-- Default ViewResolver -->       <bean id="viewResolver"                         value="org.springframework.web.servlet.view.JstlView" />           <property name="prefix" value="/" />           <property name="suffix" value=".jsp"></property>       </bean>             <bean id="messageSource"                     p:basename="i18n/messages" />   </beans>  



action
  view plaincopy to clipboardprint?package com.zuxiang.web;   import javax.annotation.Resource;   import javax.servlet.http.HttpServletRequest;   import javax.servlet.http.HttpServletResponse;   import javax.servlet.http.HttpSession;   import org.springframework.stereotype.Controller;   import org.springframework.web.bind.annotation.RequestMapping;   import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;   @Controller   @RequestMapping("/user")//可以不要,那请求的路径就是“/”   public class UserAction {      //也可以用实体类   /*      @RequestMapping("/login")      public String preHandle(HttpServletRequest arg0, HttpServletResponse arg1,              User user) throws Exception {          // TODO Auto-generated method stub         if("zx".equals(user.getName)&&"zz".equals(user.getPassword)){             System.out.println("ok");             return "ok";         }else{             return "error";         }                                   }    */          @RequestMapping("/login")       public String preHandle(HttpServletRequest arg0, HttpServletResponse arg1,               String name ,String password) throws Exception {           // TODO Auto-generated method stub          if("zx".equals(name)&&"zz".equals(password)){              System.out.println("ok");              return "ok";          }else{              return "error";          }                                       }   }   


login.jsp

view plaincopy to clipboardprint?<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>   <%   String path = request.getContextPath();   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   %>   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   <html>     <head>       <base href="<%=basePath%>">             <title>My JSP 'admin.jsp' starting page</title>             <meta http-equiv="pragma" content="no-cache">       <meta http-equiv="cache-control" content="no-cache">       <meta http-equiv="expires" content="0">          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">       <meta http-equiv="description" content="This is my page">       <!--       <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">       -->     </head>         <body>                    <div id="content">                           <h2>SpringMVCDemo示例</h2>                    <h3>--CRUD管理界面演示</h3>                    <form id="loginForm" action="user/login.do" method="post">                        <table />                                </td>                            </tr>                            <tr>                                <td colspan='2'>                                                                      <input value="登录" type="submit" />                                </td>                            </tr>                        </table>                    </form>                </div>            </body>    </html>

读书人网 >VC/MFC

热点推荐