龙空技术网

JAVA-springmvc简易Demo

浪漫的fish 422

前言:

现时各位老铁们对“java的demo”可能比较关切,兄弟们都想要知道一些“java的demo”的相关知识。那么小编在网摘上搜集了一些关于“java的demo””的相关内容,希望小伙伴们能喜欢,兄弟们一起来了解一下吧!

1、springmvc的原理:利用servlet请求与注解机制实现页面不同的业务处理,它与struts2的主要区别是:struts2是类处理级别,每个业务请求需要特定类的execute()方法进行处理,而springmvc是方法级处理,每个业务请求可以通过同一个类的方法的@RequestMapping注解进行区分处理。

2、依赖的jar包:

3、工程结构:

4、User/Product两个实体类(因篇幅简化):

public class User {

private int userid;

private String username;

private String password;

set和get方法,以及构造方法

}

public class Product {

private int prdid;

private String prdname;

private String prdtype;

private String prdprice;

set和get方法,以及构造方法

5、UserController.java控制器:实现页面不同的业务请求处理:

package com.springmvcDemo.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

import com.springmvcDemo.domain.Product;

import com.springmvcDemo.domain.User;

@Controller

@RequestMapping(value = "/user")

public class UserController {

ModelAndView modelAndView = new ModelAndView();

@RequestMapping(value = "/register", method = RequestMethod.POST)

public String register(@RequestParam("password") String password,

@RequestParam("username") String username) {

User user = new User(username, password);

System.out.println(user);

return "register";

}

@RequestMapping(value = "/login")

public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password,

Model model) {

User user = new User(username, password);

model.addAttribute("username", user.getUsername());

Product product1 = new Product(1,"商品1", "衣服", "100");

Product product2 = new Product(2,"商品2", "电器", "200");

List<Product> prdlist = new ArrayList<Product>();

prdlist.add(product1);

prdlist.add(product2);

modelAndView.addObject("prdlist", prdlist);

modelAndView.setViewName("welcome");

return modelAndView;

}

}

6、springmvc.xml:springmvc控制器配置,主要是实现类的注册与页面路径的配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=";

xmlns:xsi="; xmlns:mvc=";

xmlns:context=";

xsi:schemaLocation="

;>

<!-- 使用基于注解的控制器,spring会自动扫描base-package下的子包和类,如果扫描到会把这些类注册为bean -->

<context:component-scan base-package="com.springmvcDemo.*" />

<!-- 配置处理映射器和处理器适配器 在Spring4.0后,不配置,会默认加载 -->

<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->

<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

<!-- 配置视图解析器,经过视图解析器后,视图的的完成路径为[prefix]+返回的视图字符串+[suffix] -->

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/view/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

</beans>

7、web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=";

xmlns=";

xsi:schemaLocation=" ;

id="WebApp_ID" version="3.0">

<filter>

<filter-name>characterEncodingFilter</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>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>characterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<servlet>

<servlet-name>SpringMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 配置SpringMVC下的配置文件位置及名称 -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>SpringMVC</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

8、index.jsp :

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8">

<%@ taglib prefix="c" uri=";%>

<title>My JSP 'index.jsp' starting page</title>

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<form name="login_form" action="user" method="post">

<table>

<tr>

<td>用户名:</td>

<td><input name="userid" type="text" /></td>

</tr>

<tr>

<td>姓 名 :</td>

<td><input name="username" type="text" /></td>

</tr>

<tr>

<td>密 码 :</td>

<td><input name="password" type="text" /></td>

</tr>

<tr>

<td align="right"><input type="submit" value="登陆" onclick="logup()" /></td>

<td align="center"><input type="submit" value="注册" onclick="register()" /></td>

</tr>

</table>

</form>

<script type="text/javascript">

function logup() {

document.login_form.action = "user/login";

element.submit();

}

function register() {

document.login_form.action = "user/register";

element.submit();

}

</script>

</body>

</html>

9、welcome.jsp登陆成功的页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ";>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%@ taglib prefix="c" uri=";%>

<title>欢迎页面</title>

</head>

<body>

欢迎${requestScope.username}用户登陆成功

<table border="1" cellspacing="0" cellpadding="0">

<tr align="center">

<td>商品编号</td>

<td>商品名称</td>

<td>商品类型</td>

<td>商品价格</td>

</tr>

<c:forEach items="${prdlist}" var="product">

<tr>

<td>${product.prdid}</td>

<td>${product.prdname}</td>

<td>${product.prdtype}</td>

<td>${product.prdprice}</td>

<td><a

href="${pageContext.request.contextPath}/product/queryproduct?id=${product.prdid}">查询</a></td>

</tr>

</c:forEach>

</table>

</body>

</html>

标签: #java的demo