springmvc登陆例子详情解析
来源:互联网 发布:mysql存储过程 编辑:程序博客网 时间:2024/06/16 13:10
springmvc经典的例子就是用户登陆和用户注册,可以说通过这两个例子,你就可以对spring有一个简单的了解。也可以和struts框架做一个比较。
接下来通过代码和文字来详细的解析一下springmvc登陆小例子。
首先是创建一个项目,本例都是在myeclipse环境开发
增加spring支持jar包
选择两个标红的jar,当然你也可以通过过在官网(https://spring.io/)下载最新的jar来引入你的项目也是可以的,这种方式比较自由,myeclipse自动加载的jar包并不是最新的jar.
首先开发view视图层:index.jsp.
error.jsp.success.jsp文件可以自行开发,只要能够标记即可。
核心代码:
<body> <form action="login.do" method="post"> username:<input type="text" name = "username" ><p> password:<input type="password" name = "password" ><p> <input type="submit" value="登录"> </form> <br> </body>
控制层代码:
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class LoginAction { @RequestMapping("login.do") public String login(String username,String password){ if ("test".equals(username) && "123".equals(password)) { System.out.println(username +" 登录成功"); return "success";// } return "error"; } }
业务层和模型层通过heibernate和mybatis框架来实现。由于是一个简单的入门例子,所以相对做的简单一点。接下来会做相关深层次的例子。
spmvc-servlet.xml文件配置代码该文件放在src目录下。
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 启用spring mvc注解 --> <context:annotation-config></context:annotation-config> <!-- 扫描包 --> <context:component-scan base-package="com.mvc.*"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"></bean></beans>
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"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
最终运行结果:
1 0
- springmvc登陆例子详情解析
- SpringMVC登陆
- Notification使用详情解析
- Struts详情解析
- Springmvc框架配置和详情
- SpringMVC例子
- springMVC例子
- 登陆解析
- 景点详情JSON接口解析
- ANDROID STUDIO 2.2详情解析
- Nginx 日志配置详情解析
- springmvc登陆拦截案例
- SpringMVC登陆小demo
- springMVC登陆拦截器
- SpringMVC登陆设计流程
- jsp核心标签库详情例子
- 单一登陆的例子
- 单点登陆小例子
- 【算法】程序猿不写代码是不对的53
- configure: error: no acceptable C compiler found in $PATH 问题解决
- Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'
- Spring各个jar包作用
- 利用python进行数据分析——p26,"一定要以pylab模式”打开如何解决
- springmvc登陆例子详情解析
- Java ---多线程1
- input输入框回车事件响应
- Gson的使用
- New Day
- Kubernetes
- CentOS7部署ss
- 【剑指offer】替换空格
- 小程序开发工具不显示tobar图标