您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springmvc