SpringMVC前台向后传值(注解方式)
2018-01-24 15:23
363 查看
新建一个web项目,并导入相关jar包(指定spring的jar包,根据需要导入)。项目结构如下:
代码部分:
LoginController 类:
@Controller
public class LoginController {
private String accNum = "10010";
private String psword = "mufeng";
@RequestMapping("/login.html")
public String loginPage(){
return "login";
}
//方式一,在参数前面加注解,这样参数的命名就没有特别要求,Model参数不是必须的
@RequestMapping("/login")
public String doLogin(@RequestParam("accountNum") String account,
@RequestParam("password") String passwor,Model model){
if(account.equals(accNum) && passwor.equals(psword)){
model.addAttribute( "accountNum", account);
return "show";
}else{
return "error";
}
}
////方式二,通过界面间互相传值,这样的话参数中的accountNum及password都需要与jsp中的相同
// @RequestMapping("/login")
// public String doLogin( String accountNum, String password){
//
// if(accountNum.equals(accNum) && password.equals(psword)){
// return "show";
// }else{
// return "error";
// }
//
// }
}
config1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 这个文件是配合web.xml中的<listener>(spring监听器)使用的,虽然这里面只有一句话,
但至关重要,表示通过扫描这个包下各个类里面的注解,生成对应的servlet进行相应的操作
-->
<context:component-scan base-package="com.mufeng.controller" />
</beans>
servlet1.xml:
<?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:mvc="http://www.springframework.org/schema/mvc"
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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- scan the package and the sub package -->
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.mufeng.controller"></context:component-scan>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 转到指定的jsp页面
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="ViewResolver1">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springLearn0727-2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 如项目要用到注解的话需要加spring监听
-->
<!-- 注解监听从这里开始 -->
<listener>
<description>Spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config1.xml</param-value>
</context-param>
&l
a173
t;!-- 注解监听到这里结束 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet1.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
login.jsp中的form部分:
<form class="form-horizontal" style="font-family:
微软雅黑;"
action="login" method="post">
<div class="col-md-12"
style="padding-bottom: 2em;padding-top: 2em ">
<input type="number" class="form-control" id="exampleInputName"
placeholder="账号/手机号/QQ/微信号"
name="accountNum">
</div>
<div class="col-md-12" style="padding-bottom: 1em ">
<input type="password" class="form-control" id="inputPassword"
placeholder="密码" name="password">
</div>
<div class="col-md-12" style="padding-bottom: 1em ">
<div class="checkbox">
<label> <input type="checkbox"> <font size="2">记住我的选择</font>
</label>
</div>
</div>
<div class="col-md-12" style="padding-bottom: 2em ">
<button type="submit" class="btn btn-primary">
<font size="3">登录</font>
</button>
</div>
<div class="col-md-12" style="padding-bottom: 2em ">
<li style="padding-top:23px;" class="clearfix">其他账号登陆:</li>
</div>
</form>
代码部分:
LoginController 类:
@Controller
public class LoginController {
private String accNum = "10010";
private String psword = "mufeng";
@RequestMapping("/login.html")
public String loginPage(){
return "login";
}
//方式一,在参数前面加注解,这样参数的命名就没有特别要求,Model参数不是必须的
@RequestMapping("/login")
public String doLogin(@RequestParam("accountNum") String account,
@RequestParam("password") String passwor,Model model){
if(account.equals(accNum) && passwor.equals(psword)){
model.addAttribute( "accountNum", account);
return "show";
}else{
return "error";
}
}
////方式二,通过界面间互相传值,这样的话参数中的accountNum及password都需要与jsp中的相同
// @RequestMapping("/login")
// public String doLogin( String accountNum, String password){
//
// if(accountNum.equals(accNum) && password.equals(psword)){
// return "show";
// }else{
// return "error";
// }
//
// }
}
config1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 这个文件是配合web.xml中的<listener>(spring监听器)使用的,虽然这里面只有一句话,
但至关重要,表示通过扫描这个包下各个类里面的注解,生成对应的servlet进行相应的操作
-->
<context:component-scan base-package="com.mufeng.controller" />
</beans>
servlet1.xml:
<?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:mvc="http://www.springframework.org/schema/mvc"
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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- scan the package and the sub package -->
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.mufeng.controller"></context:component-scan>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 转到指定的jsp页面
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="ViewResolver1">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springLearn0727-2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 如项目要用到注解的话需要加spring监听
-->
<!-- 注解监听从这里开始 -->
<listener>
<description>Spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config1.xml</param-value>
</context-param>
&l
a173
t;!-- 注解监听到这里结束 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet1.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
login.jsp中的form部分:
<form class="form-horizontal" style="font-family:
微软雅黑;"
action="login" method="post">
<div class="col-md-12"
style="padding-bottom: 2em;padding-top: 2em ">
<input type="number" class="form-control" id="exampleInputName"
placeholder="账号/手机号/QQ/微信号"
name="accountNum">
</div>
<div class="col-md-12" style="padding-bottom: 1em ">
<input type="password" class="form-control" id="inputPassword"
placeholder="密码" name="password">
</div>
<div class="col-md-12" style="padding-bottom: 1em ">
<div class="checkbox">
<label> <input type="checkbox"> <font size="2">记住我的选择</font>
</label>
</div>
</div>
<div class="col-md-12" style="padding-bottom: 2em ">
<button type="submit" class="btn btn-primary">
<font size="3">登录</font>
</button>
</div>
<div class="col-md-12" style="padding-bottom: 2em ">
<li style="padding-top:23px;" class="clearfix">其他账号登陆:</li>
</div>
</form>
相关文章推荐
- SpringMVC注解---前台向后台传值的两种方式
- springMVC注解前台像后台传值的两种方式
- 防止SpringMVC注解方式的XSS攻击的方法
- springMvc 将对象json返回时自动忽略掉对象中的特定属性的注解方式
- 在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取。这里主要讲这个注解
- SpringMVC 提交参数的方式和注解详述
- springmvc4注解方式整合elasticsearch
- SpringMVC经典系列-04基于Spring3.1注解的方式进行项目开发---【LinusZhu】
- SpringMVC后台接收前台传来数据的三种简单方式
- springMVC入门搭建(注解方式及注解应用)
- SpringMVC常用注解,返回方式,路径匹配形式,验证【转】
- 浅谈SpringMVC jsp前台获取参数的方式 EL表达式
- SpringMVC系列(四)注解方式上传文件
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
- springMVC 注解方式实现全程+文件上传
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
- springMVC 注解方式实现全程+文件上传
- JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
- SpringMVC——注解的使用与结果跳转方式
- SpringMVC非注解方式学习